Universal Gates making using Reversible Computing in QISKIT
Published: 2024-03-10 22:12:54

There are different types of classical gates like AND, OR , NOT, NOR, NAND etc. If we think about it classically , Only the NOT gate is reversible and rests are not. 
By reversible I meant we can get the input value by looking at the value of output. If we look at the truth table of **NOT gate**,

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g0qidvcv2yx5gvjlbpiw.png)

Here you can infer the input or the output value by looking at the other . 

But this is not the case with other gates. To understand it clearly , let's take **OR gate** truth table,


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2nuuewe8ekzl6fblvzuh.png)

Here , suppose by using or gate on 2 unknown bits you got the output 1, now can you infer from here the input values of 2 bits ? No you can't . It is because there can be 3 possibilites like 01,10,11.

But we can do this in Quantum Computer. Quantum Computer is reversible according to the laws of physics .  Quantum Gates U are invertible (U^-1 = U^t) and hence quantum circuit (without measurement) are invertible . The idea for making it reversible is whenever you compute a gate save the inputs. So, And & OR gates should be implemented in a reversible manner . The idea is to create a 3-qubit circuit which donot modify the input bits and writes the output of 3rd bit.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2xv2ef28jc21elb2mgzg.jpg)

### Reversible AND gates implementaion:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0yce5nogs6dhwxg11nls.jpg)

It is implemented using a Toffoli gate , It is revertible in the sense that given x1,x2 and y (xor) (x1 and x2) , we can compute back y.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pdqapjz1yoh7l5weer3r.jpg)

Now the Qiskit Implementation, 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w2eez50ep5ie2egbiuxe.png)


The output of the code will be , 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/17j02vu9nvpybckoqqow.png)



### OR gate Implementation: 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nx0gn092u670awh6kmj1.jpg)

Now there can be a lot of ways to implement this, 
I am showing here two method.

**Method 1: By using the rule A xor B xor AB = A OR B**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lyz6naydpkerk9lv5ej1.jpg)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bbj1vafpeh8ayw2owqkj.png)


**Method 2: Simply negating and using De-Morgan**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/52eycdgz9qblq76b0oj6.jpg)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q90zg90mljjwtbjr26lm.png)

### XOR gate Implementation:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k6immnq2l2mdo5hhe1qx.jpg)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/le1ixjm4qpjn4fwdzl0s.png)