 Trusted Notebook" width="500 px" align="left">

## _*Basic Quantum Operations*_ 

The latest version of this notebook is available on https://github.com/QISKit/qiskit-tutorial.

***
### Contributors
Jay Gambetta, Rudy Raymond and Ali Javadi

 In this notebook we will go into the different operations that are available in QISKit-core. These are:
* Single-qubit quantum gates
* Multi-qubit quantum gates
* Measurements
* Reset
* Conditionals
* State initialization

We will also show you how to use the three different simulators:
* local_unitrary_simulator
* local_qasm_simulator
* local_statevector_simulator


```python
import sys
try:
    sys.path.append("../../") # go to parent dir
    import Qconfig
    qx_config = {
        "APItoken": Qconfig.APItoken,
        "url": Qconfig.config['url']}
except:
    qx_config = {
        "APItoken":"YOUR_TOKEN_HERE",
        "url":"https://quantumexperience.ng.bluemix.net/api"}
```


```python
# Useful additional packages 
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
from math import pi
```


```python
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister, QISKitError
from qiskit import available_backends, execute, register, get_backend
from qiskit.tools.visualization import circuit_drawer
```

## Single Qubit Quantum states

A single qubit quantum state can be written as

$$|\psi\rangle = \alpha|0\rangle + \beta |1\rangle$$


where $\alpha$ and $\beta$ are complex numbers. In a measurement the probability of the bit being in $|0\rangle$ is $|\alpha|^2$ and $|1\rangle$ is $|\beta|^2$. As a vector this is

$$
|\psi\rangle =  
\begin{pmatrix}
\alpha \\
\beta
\end{pmatrix}.
$$

Note due to conservation probability $|\alpha|^2+ |\beta|^2 = 1$ and since global phase is undetectable $|\psi\rangle := e^{i\delta} |\psi\rangle$ we only requires two real numbers to describe a single qubit quantum state.

A convenient representation is

$$|\psi\rangle = \cos(\theta/2)|0\rangle + \sin(\theta/2)e^{i\phi}|1\rangle$$

where $0\leq \phi < 2\pi$, and $0\leq \theta \leq \pi$.  From this it is clear that there is a one-to-one correspondence between qubit states ($\mathbb{C}^2$) and the points on the surface of a unit sphere ($\mathbb{R}^3$). This is called the Bloch sphere representation of a qubit state.

Quantum gates/operations are usually represented as matrices. A gate which acts on a qubit is represented by a $2\times 2$ unitary matrix $U$. The action of the quantum gate is found by multiplying the matrix representing the gate with the vector which represents the quantum state.

$$|\psi'\rangle = U|\psi\rangle$$

A general unitary must be able to take the $|0\rangle$ to the above state. That is 

$$
U = \begin{pmatrix}
\cos(\theta/2) & a \\
e^{i\phi}\sin(\theta/2) & b 
\end{pmatrix}
$$ 

where $a$ and $b$ are complex numbers constrained such that $U^\dagger U = I$ for all $0\leq\theta\leq\pi$ and $0\leq \phi<2\pi$. This gives 3 constraints and as such $a\rightarrow -e^{i\lambda}\sin(\theta/2)$ and $b\rightarrow e^{i\lambda+i\phi}\cos(\theta/2)$ where $0\leq \lambda<2\pi$ giving 

$$
U = \begin{pmatrix}
\cos(\theta/2) & -e^{i\lambda}\sin(\theta/2) \\
e^{i\phi}\sin(\theta/2) & e^{i\lambda+i\phi}\cos(\theta/2) 
\end{pmatrix}.
$$

This is the most general form of a single qubit unitary.

## Single-Qubit Gates

The single-qubit gates available are:
* u gates
* Identity gate
* Pauli gates
* Cliffords gates
* $C3$ gates
* Standard rotation gates 

We have provided a backend: `local_unitary_simulator` to allow you to calculate the unitary matrices. 


```python
q = QuantumRegister(1)
```

### u gates

In QISKit we give you access to the general unitary using the $u3$ gate

$$
u3(\theta, \phi, \lambda) = U(\theta, \phi, \lambda) 
$$



```python
qc = QuantumCircuit(q)
qc.u3(pi/2,pi/2,pi/2,q)
circuit_drawer(qc)
```


```python
job = execute(qc, backend='local_unitary_simulator')
np.round(job.result().get_data(qc)['unitary'], 3)
```




    array([[ 0.707+0.j   , -0.   -0.707j],
           [ 0.   +0.707j, -0.707+0.j   ]])



The $u2(\phi, \lambda) =u3(\pi/2, \phi, \lambda)$ has the martix form

$$
u2(\phi, \lambda) = 
\frac{1}{\sqrt{2}} \begin{pmatrix}
1 & -e^{i\lambda} \\
e^{i\phi} & e^{i(\phi + \lambda)}
\end{pmatrix}.
$$

This is a useful gate as it allows us to create superpositions


```python
qc = QuantumCircuit(q)
qc.u2(pi/2,pi/2,q)
circuit_drawer(qc)
```


```python
job = execute(qc, backend='local_unitary_simulator')
np.round(job.result().get_data(qc)['unitary'], 3)
```




    array([[ 0.707+0.j   , -0.   -0.707j],
           [ 0.   +0.707j, -0.707+0.j   ]])



The $u1(\lambda)= u3(0, 0, \lambda)$ gate has the matrix form

$$
u1(\lambda) = 
\begin{pmatrix}
1 & 0 \\
0 & e^{i \lambda}
\end{pmatrix},
$$

which is a useful as it allows us to apply a quantum phase.


```python
qc = QuantumCircuit(q)
qc.u1(pi/2,q)
circuit_drawer(qc)
```


```python
job = execute(qc, backend='local_unitary_simulator')
np.round(job.result().get_data(qc)['unitary'], 3)
```




    array([[1.+0.j, 0.+0.j],
           [0.+0.j, 0.+1.j]])



The $u0(\delta)= u3(0, 0, 0)$ gate is the identity matrix. It has the matrix form

$$
u0(\delta) = 
\begin{pmatrix}
1 & 0 \\
0 & 1
\end{pmatrix}.
$$

The identity gate does nothing (but can add noise in the real device for a period of time equal to fractions of the single qubit gate time)


```python
qc = QuantumCircuit(q)
qc.u0(pi/2,q)
circuit_drawer(qc)
```


```python
job = execute(qc, backend='local_unitary_simulator')
np.round(job.result().get_data(qc)['unitary'], 3)
```




    array([[1.+0.j, 0.+0.j],
           [0.+0.j, 1.+0.j]])



### Identity gate

The identity gate is $Id = u0(1)$.


```python
qc = QuantumCircuit(q)
qc.iden(q)
circuit_drawer(qc)
```


```python
job = execute(qc, backend='local_unitary_simulator')
np.round(job.result().get_data(qc)['unitary'], 3)
```




    array([[1.+0.j, 0.+0.j],
           [0.+0.j, 1.+0.j]])



### Pauli gates

#### $X$: bit-flip gate

The bit-flip gate $X$ is defined as:

$$
X   =  
\begin{pmatrix}
0 & 1\\
1 & 0
\end{pmatrix}= u3(\pi,0,\pi)
$$


```python
qc = QuantumCircuit(q)
qc.x(q)
circuit_drawer(qc)
```


```python
job = execute(qc, backend='local_unitary_simulator')
np.round(job.result().get_data(qc)['unitary'], 3)
```




    array([[ 0.+0.j,  1.-0.j],
           [ 1.+0.j, -0.+0.j]])



#### $Y$: bit- and phase-flip gate

The $Y$ gate is defined as:

$$
Y  = 
\begin{pmatrix}
0 & -i\\
i & 0
\end{pmatrix}=u3(\pi,\pi/2,\pi/2)
$$


```python
qc = QuantumCircuit(q)
qc.y(q)
circuit_drawer(qc)
```


```python
job = execute(qc, backend='local_unitary_simulator')
np.round(job.result().get_data(qc)['unitary'], 3)
```




    array([[ 0.+0.j, -0.-1.j],
           [ 0.+1.j, -0.+0.j]])



#### $Z$: phase-flip gate

The phase flip gate $Z$ is defined as:

$$
Z = 
\begin{pmatrix}
1 & 0\\
0 & -1
\end{pmatrix}=u1(\pi)
$$


```python
qc = QuantumCircuit(q)
qc.z(q)
circuit_drawer(qc)
```


```python
job = execute(qc, backend='local_unitary_simulator')
np.round(job.result().get_data(qc)['unitary'], 3)
```




    array([[ 1.+0.j,  0.+0.j],
           [ 0.+0.j, -1.+0.j]])



### Clifford gates

#### Hadamard gate

$$
H = 
\frac{1}{\sqrt{2}}
\begin{pmatrix}
1 & 1\\
1 & -1
\end{pmatrix}= u2(0,\pi)
$$


```python
qc = QuantumCircuit(q)
qc.h(q)
circuit_drawer(qc)
```


```python
job = execute(qc, backend='local_unitary_simulator')
np.round(job.result().get_data(qc)['unitary'], 3)
```




    array([[ 0.707+0.j,  0.707-0.j],
           [ 0.707+0.j, -0.707+0.j]])



#### $S$ (or, $\sqrt{Z}$ phase) gate

$$
S = 
\begin{pmatrix}
1 & 0\\
0 & i
\end{pmatrix}= u1(\pi/2)
$$


```python
qc = QuantumCircuit(q)
qc.s(q)
circuit_drawer(qc)
```


```python
job = execute(qc, backend='local_unitary_simulator')
np.round(job.result().get_data(qc)['unitary'], 3)
```




    array([[1.+0.j, 0.+0.j],
           [0.+0.j, 0.+1.j]])



#### $S^{\dagger}$ (or, conjugate of $\sqrt{Z}$ phase) gate

$$
S^{\dagger} = 
\begin{pmatrix}
1 & 0\\
0 & -i
\end{pmatrix}= u1(-\pi/2)
$$



```python
qc = QuantumCircuit(q)
qc.sdg(q)
circuit_drawer(qc)
```


```python
job = execute(qc, backend='local_unitary_simulator')
np.round(job.result().get_data(qc)['unitary'], 3)
```




    array([[1.+0.j, 0.+0.j],
           [0.+0.j, 0.-1.j]])



### $C3$ gates
#### $T$ (or, $\sqrt{S}$ phase) gate

$$
T = 
\begin{pmatrix}
1 & 0\\
0 & e^{i \pi/4}
\end{pmatrix}= u1(\pi/4) 
$$


```python
qc = QuantumCircuit(q)
qc.t(q)
circuit_drawer(qc)
```


```python
job = execute(qc, backend='local_unitary_simulator')
np.round(job.result().get_data(qc)['unitary'], 3)
```




    array([[1.   +0.j   , 0.   +0.j   ],
           [0.   +0.j   , 0.707+0.707j]])



#### $T^{\dagger}$ (or, conjugate of $\sqrt{S}$ phase) gate

$$
T^{\dagger} =  
\begin{pmatrix}
1 & 0\\
0 & e^{-i \pi/4}
\end{pmatrix}= u1(-pi/4)
$$

They can be added as below.


```python
qc = QuantumCircuit(q)
qc.tdg(q)
circuit_drawer(qc)
```


```python
job = execute(qc, backend='local_unitary_simulator')
np.round(job.result().get_data(qc)['unitary'], 3)
```




    array([[1.   +0.j   , 0.   +0.j   ],
           [0.   +0.j   , 0.707-0.707j]])



### Standard Rotations

The standard rotation gates are those that define rotations around the Paulis $P=\{X,Y,Z\}$. They are defined as 

$$ R_P(\theta) = \exp(-i \theta P/2) = \cos(\theta/2)I -i \sin(\theta/2)P$$

#### Rotation around X-axis

$$
R_x(\theta) = 
\begin{pmatrix}
\cos(\theta/2) & -i\sin(\theta/2)\\
-i\sin(\theta/2) & \cos(\theta/2)
\end{pmatrix} = u3(\theta, -\pi/2,\pi/2)
$$


```python
qc = QuantumCircuit(q)
qc.rx(pi/2,q)
circuit_drawer(qc)
```


```python
job = execute(qc, backend='local_unitary_simulator')
np.round(job.result().get_data(qc)['unitary'], 3)
```




    array([[ 0.707+0.j   , -0.   -0.707j],
           [ 0.   -0.707j,  0.707+0.j   ]])



#### Rotation round Y-axis

$$
R_y(\theta) =
\begin{pmatrix}
\cos(\theta/2) & - \sin(\theta/2)\\
\sin(\theta/2) & \cos(\theta/2).
\end{pmatrix} =u3(\theta,0,0)
$$


```python
qc = QuantumCircuit(q)
qc.ry(pi/2,q)
circuit_drawer(qc)
```


```python
job = execute(qc, backend='local_unitary_simulator')
np.round(job.result().get_data(qc)['unitary'], 3)
```




    array([[ 0.707+0.j, -0.707+0.j],
           [ 0.707+0.j,  0.707+0.j]])



#### Rotation around Z-axis

$$
R_z(\phi) = 
\begin{pmatrix}
e^{-i \phi/2} & 0 \\
0 & e^{i \phi/2}
\end{pmatrix}\equiv u1(\phi)
$$

Note here we have used an equivalent as is different to u1 by global phase $e^{-i \phi/2}$.


```python
qc = QuantumCircuit(q)
qc.rz(pi/2,q)
circuit_drawer(qc)
```


```python
job = execute(qc, backend='local_unitary_simulator')
np.round(job.result().get_data(qc)['unitary'], 3)
```




    array([[1.+0.j, 0.+0.j],
           [0.+0.j, 0.+1.j]])



Note this is different due only to a global phase

## Multi-Qubit Gates

The space of quantum computer grows exponential with the number of qubits. For $n$ qubits the complex vector space has dimensions $d=2^n$. 

Notice that in the QISKit, the order of qubits are represented from the most significant bit (MSB) on the left to the least significant bit (LSB) on the right (big-endian). This is similar to bitstring representation on classical computers, and enables easy conversion from bitstrings to integers after measurements are performed.  That is the first four states of two qubits are $|00\rangle$, $|01\rangle$, $|10\rangle$, and $|11\rangle$.

The joint space is a tensor product space:
\begin{equation}
	X\otimes Y = 
	\begin{pmatrix} 
		x_{00} \begin{pmatrix} 
			y_{00} & y_{01} \\
			y_{10} & y_{11}
		\end{pmatrix} & x_{01} 	\begin{pmatrix} 
				y_{00} & y_{01} \\
				y_{10} & y_{11}
			\end{pmatrix} \\
		x_{10} 	\begin{pmatrix} 
					y_{00} & y_{01} \\
					y_{10} & y_{11}
				\end{pmatrix} & x_{11} 	\begin{pmatrix} 
							y_{00} & y_{01} \\
							y_{10} & y_{11}
						\end{pmatrix}
	\end{pmatrix}						
\end{equation} which means for the states we have 
\begin{equation}\begin{split}
	|{00}\rangle &= \begin{pmatrix} 
		\begin{pmatrix} 
			1  \\
			0
		\end{pmatrix} \\
		\begin{pmatrix} 
			0  \\
			0 
		\end{pmatrix}
	\end{pmatrix} ~  	|{01}\rangle = \begin{pmatrix} 
	\begin{pmatrix} 
	0 \\
	1
	\end{pmatrix} \\
	\begin{pmatrix} 
	0  \\
	0 
	\end{pmatrix}
	\end{pmatrix} ~ 	|{10}\rangle = \begin{pmatrix} 
	\begin{pmatrix} 
	0  \\
	0
	\end{pmatrix} \\
	\begin{pmatrix} 
	1 \\
	0 
	\end{pmatrix}
	\end{pmatrix} ~ 	|{11}\rangle = \begin{pmatrix} 
	\begin{pmatrix} 
	0  \\
	0
	\end{pmatrix} \\
	\begin{pmatrix} 
	0  \\
	1 
	\end{pmatrix}
	\end{pmatrix}\end{split}
\end{equation}

Thus, we write the tensor product of qubits $q_0$, $q_1$, and $q_2$ as $q_2 \otimes q_1 \otimes q_0$. The physics community often writes this in the opposite way, and care must be taken when working with these scenarios, as demonstrated in multi-qubit gates below.

A common two-qubit gate is $C_U$. The $C_U$ when qubit 0 is the control and qubit 1 is the target (eg `cU(q[0],q[1])`) is

\begin{equation}
	C_U = \begin{pmatrix}
	1 & 0 & 0 & 0 \\
	0 & u_{00} & 0 & u_{01} \\
	0 & 0 & 1 & 0 \\
	0 & u_{10} &0 & u_{11}
		\end{pmatrix},
\end{equation} 


The $C_U$ when qubit 1 is the control and qubit 0 is the taget (eg `cU(q[1],q[0])`)
\begin{equation}
	C_U = \begin{pmatrix}
	1 & 0 & 0  & 0 \\
	0 & 1 & 0 & 0 \\
	0 & 0 & u_{00} & u_{01} \\
	0 & 0 & u_{10} & u_{11}
		\end{pmatrix}
\end{equation}

The standard multi-qubit gates consist of two-qubit gates and three-qubit gates. The two-qubit gates are:
* controlled Pauli gates
* controlled Hadamard gate
* controlled rotation gates
* controlled phase gate
* controlled u3 gate
* swap gate

The three-qubit gates are: 
* Toffoli gate 
* Fredkin gate



```python
q = QuantumRegister(2)
```

### Controlled Pauli Gates

#### Controlled-X (or, controlled-NOT) gate
The controlled-not gate flips the `target` qubit when the control qubit is in the state $|1\rangle$. If we take the MSB as the control qubit (e.g. `cx(q[1],q[0])`), then the matrix would look like

$$
C_X = 
\begin{pmatrix}
1 & 0 & 0 & 0\\
0 & 1 & 0 & 0\\
0 & 0 & 0 & 1\\
0 & 0 & 1 & 0
\end{pmatrix}. 
$$

However, when the LSB is the control qubit, (e.g. `cx(q[0],q[1])`), this gate is equivalent to the following matrix:

$$
C_X = 
\begin{pmatrix}
1 & 0 & 0 & 0\\
0 & 0 & 0 & 1\\
0 & 0 & 1 & 0\\
0 & 1 & 0 & 0
\end{pmatrix}. 
$$




```python
qc = QuantumCircuit(q)
qc.cx(q[0],q[1])
circuit_drawer(qc)
```


```python
job = execute(qc, backend='local_unitary_simulator')
np.round(job.result().get_data(qc)['unitary'], 3)
```




    array([[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
           [0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j],
           [0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j],
           [0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j]])



#### Controlled $Y$ gate

Apply the $Y$ gate to the target qubit if the control qubit is the MSB

$$
C_Y = 
\begin{pmatrix}
1 & 0 & 0 & 0\\
0 & 1 & 0 & 0\\
0 & 0 & 0 & -i\\
0 & 0 & i & 0
\end{pmatrix},
$$

or when the LSB is the control

$$
C_Y = 
\begin{pmatrix}
1 & 0 & 0 & 0\\
0 & 0 & 0 & -i\\
0 & 0 & 1 & 0\\
0 & i & 0 & 0
\end{pmatrix}.
$$


```python
qc = QuantumCircuit(q)
qc.cy(q[0],q[1])
circuit_drawer(qc)
```


```python
job = execute(qc, backend='local_unitary_simulator')
np.round(job.result().get_data(qc)['unitary'], 3)
```




    array([[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
           [0.+0.j, 0.+0.j, 0.+0.j, 0.-1.j],
           [0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j],
           [0.+0.j, 0.+1.j, 0.+0.j, 0.+0.j]])



#### Controlled $Z$ (or, controlled Phase) gate

Similarly, the controlled Z gate flips the phase of the target qubit if the control qubit is $1$. The matrix looks the same regardless of whether the MSB or LSB is the control qubit:

$$
C_Z = 
\begin{pmatrix}
1 & 0 & 0 & 0\\
0 & 1 & 0 & 0\\
0 & 0 & 1 & 0\\
0 & 0 & 0 & -1
\end{pmatrix}
$$



```python
qc = QuantumCircuit(q)
qc.cz(q[0],q[1])
circuit_drawer(qc)
```


```python
job = execute(qc, backend='local_unitary_simulator')
np.round(job.result().get_data(qc)['unitary'], 3)
```




    array([[ 1.-0.j,  0.+0.j,  0.+0.j,  0.+0.j],
           [ 0.+0.j,  1.-0.j,  0.+0.j, -0.-0.j],
           [ 0.+0.j,  0.+0.j,  1.-0.j,  0.+0.j],
           [ 0.+0.j, -0.+0.j,  0.+0.j, -1.+0.j]])



### Controlled Hadamard gate

Apply $H$ gate to the target qubit if the control qubit is $|1\rangle$. Below is the case where the control is the LSB qubit.

$$
C_H = 
\begin{pmatrix}
1 & 0 & 0 & 0\\
0 & \frac{1}{\sqrt{2}} & 0 & \frac{1}{\sqrt{2}}\\
0 & 0 & 1 & 0\\
0 & \frac{1}{\sqrt{2}}  & 0& -\frac{1}{\sqrt{2}}
\end{pmatrix}
$$


```python
qc = QuantumCircuit(q)
qc.ch(q[0],q[1])
circuit_drawer(qc)
```


```python
job = execute(qc, backend='local_unitary_simulator')
np.round(job.result().get_data(qc)['unitary']/(0.707+0.707j), 3)
```




    array([[ 1.   -0.j,  0.   +0.j, -0.   +0.j,  0.   +0.j],
           [ 0.   +0.j,  0.707-0.j,  0.   +0.j,  0.707-0.j],
           [ 0.   -0.j,  0.   +0.j,  1.   -0.j,  0.   +0.j],
           [ 0.   +0.j,  0.707-0.j,  0.   +0.j, -0.707+0.j]])



### Controlled rotation gates

#### Controlled rotation around Z-axis

Perform rotation around Z-axis on the target qubit if the control qubit (here LSB) is $|1\rangle$.

$$
C_{Rz}(\lambda) = 
\begin{pmatrix}
1 & 0 & 0 & 0\\
0 & e^{-i\lambda/2} & 0 & 0\\
0 & 0 & 1 & 0\\
0 & 0 & 0 & e^{i\lambda/2}
\end{pmatrix}
$$


```python
qc = QuantumCircuit(q)
qc.crz(pi/2,q[0],q[1])
circuit_drawer(qc)
```


```python
job = execute(qc, backend='local_unitary_simulator')
np.round(job.result().get_data(qc)['unitary'], 3)
```




    array([[1.   +0.j   , 0.   +0.j   , 0.   +0.j   , 0.   +0.j   ],
           [0.   +0.j   , 0.707-0.707j, 0.   +0.j   , 0.   +0.j   ],
           [0.   +0.j   , 0.   +0.j   , 1.   +0.j   , 0.   +0.j   ],
           [0.   +0.j   , 0.   +0.j   , 0.   +0.j   , 0.707+0.707j]])



### Controlled phase rotation

Perform a phase rotation if both qubits are in the $|11\rangle$ state. The matrix looks the same regardless of whether the MSB or LSB is the control qubit.

$$
C_{u1}(\lambda) = 
\begin{pmatrix}
1 & 0 & 0 & 0\\
0 & 1 & 0 & 0\\
0 & 0 & 1 & 0\\
0 & 0 & 0 & e^{i\lambda}
\end{pmatrix}
$$


```python
qc = QuantumCircuit(q)
qc.cu1(pi/2,q[0], q[1])
circuit_drawer(qc)
```

I THINK SHOULD BE CALLED $C_\mathrm{PHASE}(\lambda)$


```python
job = execute(qc, backend='local_unitary_simulator')
np.round(job.result().get_data(qc)['unitary'], 3)
```




    array([[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
           [0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j],
           [0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j],
           [0.+0.j, 0.+0.j, 0.+0.j, 0.+1.j]])



### Controlled $u3$ rotation

Perform controlled-$u3$ rotation on the target qubit if the control qubit (here LSB) is $|1\rangle$. 

$$
C_{u3}(\theta, \phi, \lambda) \equiv 
\begin{pmatrix}
1 & 0 & 0 & 0\\
0 & e^{-i(\phi+\lambda)/2}\cos(\theta/2) & 0 & -e^{-i(\phi-\lambda)/2}\sin(\theta/2)\\
0 & 0 & 1 & 0\\
0 & e^{i(\phi-\lambda)/2}\sin(\theta/2) & 0 & e^{i(\phi+\lambda)/2}\cos(\theta/2)
\end{pmatrix}.
$$


```python
qc = QuantumCircuit(q)
qc.cu3(pi/2, pi/2, pi/2, q[0], q[1])
circuit_drawer(qc)
```


```python
job = execute(qc, backend='local_unitary_simulator')
np.round(job.result().get_data(qc)['unitary'], 3)
```




    array([[ 1.   +0.j   ,  0.   +0.j   ,  0.   +0.j   ,  0.   +0.j   ],
           [ 0.   +0.j   ,  0.   -0.707j,  0.   +0.j   , -0.707+0.j   ],
           [ 0.   +0.j   ,  0.   +0.j   ,  1.   +0.j   ,  0.   +0.j   ],
           [ 0.   +0.j   ,  0.707+0.j   ,  0.   +0.j   ,  0.   +0.707j]])



NOTE I NEED TO FIX THIS AND DECIDE ON CONVENTION - I ACTUALLY THINK WE WANT A FOUR PARAMETER GATE AND JUST CALL IT CU AND TO REMOVE THIS GATE.

### Swap gate

The swap gate exchange the first and second qubits. 

$$
{SWAP} = 
\begin{pmatrix}
1 & 0 & 0 & 0\\
0 & 0 & 1 & 0\\
0 & 1 & 0 & 0\\
0 & 0 & 0 & 1
\end{pmatrix}.
$$


```python
qc = QuantumCircuit(q)
qc.swap(q[0], q[1])
circuit_drawer(qc)
```


```python
job = execute(qc, backend='local_unitary_simulator')
np.round(job.result().get_data(qc)['unitary'], 3)
```




    array([[1.+0.j, 0.+0.j, 0.+0.j, 0.+0.j],
           [0.+0.j, 0.+0.j, 1.+0.j, 0.+0.j],
           [0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j],
           [0.+0.j, 0.+0.j, 0.+0.j, 1.+0.j]])



### Toffoli gate ($ccx$ gate)

The Toffoli gate flips the third qubit if the first two qubits (LSB) are both $|1\rangle$.

$$
C_{CX} = 
\begin{pmatrix}
1 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\
0 & 1 & 0 & 0 & 0 & 0 & 0 & 0\\
0 & 0 & 1 & 0 & 0 & 0 & 0 & 0\\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 1\\
0 & 0 & 0 & 0 & 1 & 0 & 0 & 0\\
0 & 0 & 0 & 0 & 0 & 1 & 0 & 0\\
0 & 0 & 0 & 0 & 0 & 0 & 1 & 0\\
0 & 0 & 0 & 1 & 0 & 0 & 0 & 0
\end{pmatrix}
$$


```python
q = QuantumRegister(3)
```


```python
qc = QuantumCircuit(q)
qc.ccx(q[0], q[1], q[2])
circuit_drawer(qc)
```


```python
job = execute(qc, backend='local_unitary_simulator')
np.round(job.result().get_data(qc)['unitary'], 3)
```




    array([[ 1.-0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,
             0.+0.j],
           [ 0.+0.j,  1.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,
             0.+0.j],
           [ 0.+0.j,  0.+0.j,  1.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,
             0.+0.j],
           [ 0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,
             1.-0.j],
           [ 0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  1.-0.j,  0.+0.j,  0.+0.j,
             0.+0.j],
           [ 0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  1.-0.j,  0.+0.j,
             0.+0.j],
           [ 0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  1.-0.j,
             0.+0.j],
           [ 0.+0.j,  0.+0.j,  0.+0.j,  1.-0.j,  0.+0.j,  0.+0.j,  0.+0.j,
            -0.+0.j]])



### Controlled swap gate (Fredkin Gate)

The controlled swap gate exchange the second and third qubits if the first qubit (LSB) is $|1\rangle$.

$$
C_{SWAP} = 
\begin{pmatrix}
1 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\
0 & 1 & 0 & 0 & 0 & 0 & 0 & 0\\
0 & 0 & 1 & 0 & 0 & 0 & 0 & 0\\
0 & 0 & 0 & 0 & 0 & 1 & 0 & 0\\
0 & 0 & 0 & 0 & 1 & 0 & 0 & 0\\
0 & 0 & 0 & 1 & 0 & 0 & 0 & 0\\
0 & 0 & 0 & 0 & 0 & 0 & 1 & 0\\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 1
\end{pmatrix}
$$


```python
qc = QuantumCircuit(q)
qc.cswap(q[0], q[1], q[2])
circuit_drawer(qc)
```


```python
job = execute(qc, backend='local_unitary_simulator')
np.round(job.result().get_data(qc)['unitary'], 3)
```




    array([[ 1.-0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,
             0.+0.j],
           [ 0.+0.j,  1.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,
             0.+0.j],
           [ 0.+0.j,  0.+0.j,  1.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,
             0.+0.j],
           [ 0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,
             1.-0.j],
           [ 0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  1.-0.j,  0.+0.j,  0.+0.j,
             0.+0.j],
           [ 0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  1.-0.j,  0.+0.j,
             0.+0.j],
           [ 0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  0.+0.j,  1.-0.j,
             0.+0.j],
           [ 0.+0.j,  0.+0.j,  0.+0.j,  1.-0.j,  0.+0.j,  0.+0.j,  0.+0.j,
            -0.+0.j]])



## Non unitary operations

Now we have gone through all the unitary operations in quantum circuits we also have access to non-unitary operations. These include measurements, reset of qubits, and classical conditional operations.


```python
q = QuantumRegister(1)
c = ClassicalRegister(1)
```

### Measurements

We dont have access to all the information when we make a measurement in a quantum computer. The quantum state is projected onto the standard basis. Below are two examples showing a circuit that is prepared in a basis state and the quantum computer prepared in a superpostion state.


```python
qc = QuantumCircuit(q, c)
qc.measure(q, c)
circuit_drawer(qc)
```


```python
job = execute(qc, backend='local_qasm_simulator', shots=1024)
job.result().get_counts(qc)
```




    {'0': 1024}



 The simulator predicts that 100 percent of the time the classical registor returns 0. 


```python
qc = QuantumCircuit(q, c)
qc.h(q)
qc.measure(q, c)
circuit_drawer(qc)
```


```python
job = execute(qc, backend='local_qasm_simulator', shots=1024)
job.result().get_counts(qc)
```




    {'0': 524, '1': 500}



 The simulator predicts that 50 percent of the time the classical registor returns 0 or 1. 

### Reset
It is also possible to `reset` qubits to the $|0\rangle$ state in the middle of computation. Note that `reset` is not a Gate operation, since it is irreversible.


```python
qc = QuantumCircuit(q, c)
qc.reset(q[0])
qc.measure(q, c)
circuit_drawer(qc)
```


```python
job = execute(qc, backend='local_qasm_simulator', shots=1024)
job.result().get_counts(qc)
```




    {'0': 1024}




```python
qc = QuantumCircuit(q, c)
qc.h(q)
qc.reset(q[0])
qc.measure(q, c)
circuit_drawer(qc)
```


```python
job = execute(qc, backend='local_qasm_simulator', shots=1024)
job.result().get_counts(qc)
```




    {'0': 1024}



Here we see that for both of these circuits the simulator always predicts that the output is 100 percent in the 0 state.

### Conditional operations
It is also possible to do operations conditioned on the state of the classical registor


```python
qc = QuantumCircuit(q, c)
qc.x(q[0]).c_if(c, 0)
qc.measure(q,c)
circuit_drawer(qc)
```


```python
job = execute(qc, backend='local_qasm_simulator', shots=1024)
job.result().get_counts(qc)
```




    {'1': 1024}



Here the classical bit always takes the value 0 so the qubit state is always flipped. 


```python
qc = QuantumCircuit(q, c)
qc.h(q)
qc.measure(q,c)
qc.x(q[0]).c_if(c, 0)
qc.measure(q,c)
circuit_drawer(qc)
```


```python
job = execute(qc, backend='local_qasm_simulator', shots=1024)
job.result().get_counts(qc)
```




    {'1': 1024}



Here the classical bit by the first measurement is random but the conditional operation results in the qubit being deterministically put into $|1\rangle$.

### Arbitrary initialization
What if we want to initialize a qubit register to an arbitrary state? An arbitrary state for $n$ qubits may be specified by a vector of $2^n$ amplitudes, where the sum of amplitude-norms-squared equals 1. For example, the following three-qubit state can be prepared:

$$|\psi\rangle = \frac{i}{4}|000\rangle + \frac{1}{\sqrt{8}}|001\rangle + \frac{1+i}{4}|010\rangle + \frac{1+2i}{\sqrt{8}}|101\rangle + \frac{1}{4}|110\rangle$$


```python
# Initializing a three-qubit quantum state
import math
desired_vector = [
    1 / math.sqrt(16) * complex(0, 1),
    1 / math.sqrt(8) * complex(1, 0),
    1 / math.sqrt(16) * complex(1, 1),
    0,
    0,
    1 / math.sqrt(8) * complex(1, 2),
    1 / math.sqrt(16) * complex(1, 0),
    0]


q = QuantumRegister(3)

qc = QuantumCircuit(q)

qc.initialize(desired_vector, [q[0],q[1],q[2]])
circuit_drawer(qc)
```


```python
job = execute(qc, backend='local_statevector_simulator')
qc_state = job.result().get_statevector(qc)
qc_state 
```




    array([ 2.50000000e-01+0.00000000e+00j,  5.55111512e-17-3.53553391e-01j,
            2.50000000e-01-2.50000000e-01j, -2.19980517e-18-3.09536077e-17j,
           -2.62603921e-17+5.08377096e-17j,  7.07106781e-01-3.53553391e-01j,
            8.67361738e-17-2.50000000e-01j,  0.00000000e+00+0.00000000e+00j])




```python
def norm(state1, state2):
    return abs(np.dot(np.array(state1).conj(), state2))**2
```


```python
norm(desired_vector,qc_state)
```




    1.0



#### Further details:

How does the desired state get generated behind the scenes? There are multiple methods for doing this. QISKit uses a [method proposed by Shende et al](https://arxiv.org/abs/quant-ph/0406176). Here, the idea is to assume the quantum register to have started from our desired state, and construct a circuit that takes it to the $|00..0\rangle$ state. The initialization circuit is then the reverse of such circuit.

To take an arbitrary quantum state to the zero state in the computational basis, we perform an iterative procedure that disentangles qubits from the register one-by-one. We know that any arbitrary single-qubit state $|\rho\rangle$ can be taken to the $|0\rangle$ state using a $\phi$-degree rotation about the Z axis followed by a $\theta$-degree rotation about the Y axis:

$$R_y(-\theta)R_z(-\phi)|\rho\rangle = re^{it}|0\rangle$$

Since now we are dealing with $n$ qubits instead of just 1, we must factorize the state vector to separate the Least Significant Bit (LSB):

$$\begin{align*}
& |\psi\rangle = \alpha_{0_0}|00..00\rangle + \alpha_{0_1}|00..01\rangle + \alpha_{1_0}|00..10\rangle + \alpha_{1_1}|00..11\rangle + ... + \alpha_{(2^{n-1}-1)_0}|11..10\rangle + \alpha_{(2^{n-1}-1)_1}|11..11\rangle \\
&= |00..0\rangle (\alpha_{0_0}|0\rangle + \alpha_{0_1}|1\rangle) + |00..1\rangle (\alpha_{1_0}|0\rangle + \alpha_{1_1}|1\rangle) + ... + |11..1\rangle (\alpha_{(2^{n-1}-1)_0}(|0\rangle + \alpha_{(2^{n-1}-1)_1}|1\rangle) \\
&= |00..0\rangle|\rho_0\rangle + |00..1\rangle|\rho_1\rangle + ... + |11..1\rangle|\rho_{2^{n-1}-1}\rangle
\end{align*}$$

Now each of the single-qubit states $|\rho_0\rangle, ..., |\rho_{2^{n-1}-1}\rangle$ can be taken to $|0\rangle$ by finding appropriate $\phi$ and $\theta$ angles per the equation above. Doing this simultaneously on all states amounts to the following unitary, which disentangles the LSB:

$$U = \begin{pmatrix} 
R_{y}(-\theta_0)R_{z}(-\phi_0) & & & &\\  
& R_{y}(-\theta_1)R_{z}(-\phi_1) & & &\\
& . & & &\\
& & . & &\\
& & & & R_y(-\theta_{2^{n-1}-1})R_z(-\phi_{2^{n-1}-1})
\end{pmatrix} $$

Hence,

$$U|\psi\rangle = \begin{pmatrix} r_0e^{it_0}\\ r_1e^{it_1}\\ . \\ . \\ r_{2^{n-1}-1}e^{it_{2^{n-1}-1}} \end{pmatrix}\otimes|0\rangle$$


U can be implemented as a "quantum multiplexor" gate, since it is a block diagonal matrix. In the quantum multiplexor formalism, a block diagonal matrix of size $2^n \times 2^n$, and consisting of $2^s$ blocks, is equivalent to a multiplexor with $s$ select qubits and $n-s$ data qubits. Depending on the state of the select qubits, the corresponding blocks are applied to the data qubits. A multiplexor of this kind can be implemented after recursive decomposition to primitive gates of cx, rz and ry.


```python

```


```python

```

c276eda29c98b01e3eb5ee5e204f227e767def7d
ipynb
Jupyter Notebook
reference/tools/quantum_gates_and_linear_algebra.ipynb
marisvs/qiskit-tutorial
9efa21357a6f01e0312ad2e9786b1a0f9f67fc74
Apache-2.0
reference/tools/quantum_gates_and_linear_algebra.ipynb
marisvs/qiskit-tutorial
9efa21357a6f01e0312ad2e9786b1a0f9f67fc74
Apache-2.0
reference/tools/quantum_gates_and_linear_algebra.ipynb
marisvs/qiskit-tutorial
9efa21357a6f01e0312ad2e9786b1a0f9f67fc74
Apache-2.0
Qwen/Qwen-72B
1. YES
2. YES