# Introduction to Circuits and Gates

# What is a qubit?

A qubit is a quantum mechanical system consisting of a two-level system, whose state can be written as,

$∣Ψ⟩=α∣0⟩+β∣1⟩$

where $α$ and $β$ are the complex numbers, such that $∣α∣_{2}+∣β∣_{2}=1$.

Here, $∣0⟩=[10 ]$, and $∣1⟩=[01 ]$. It can be observed that a qubit can be in superposition of both $∣0⟩$ and $∣1⟩$ states. When a qubit is measured, the probabilities of getting $∣0⟩$ and $∣1⟩$ are $∣α∣_{2}$, and $∣β∣_{2}$ respectively, when added the total probability gives 1.

On a Bloch sphere (a unit sphere with radius = 1), a qubit's state can be properly visualized, where it's state is written as,

$∣Ψ⟩=cos(θ/2)∣0⟩+e_{iϕ}sin(θ/2)∣1⟩$

Here, $θ$ and $ϕ$ are the polar and azimuthal angles on the Bloch sphere, such that $θ$ varies from 0 to $π$ and $ϕ$ varies from 0 to $2π$. A unique value of ${θ,ϕ}$ponds to a particular point on the Bloch sphere, and each point on the Bloch sphere represents a unique quantum state.

## Measuring a qubit

On the Bloch sphere, there are different axes along which the qubits are measured, they were X, Y and Z axes. Correspndingly, X-basis, Y-basis and Z-basis states are written as ${∣+⟩,∣−⟩}$, ${∣l⟩,∣r⟩}$, and ${∣0⟩,∣1⟩}$ respectively. If the qubits are measured along X-axis, then either $∣+⟩$ or $∣−⟩$ results. If the qubits are measured along Y-axis, then either $∣l⟩$ or $∣r⟩$ results, similarly if the qubits are measured along Z-axis, either $∣0⟩$ or $∣1⟩$ results. It is to be noted that, $∣l⟩=2 ∣0⟩+i∣1⟩ $ and $∣r⟩=2 ∣0⟩−i∣1⟩ $.

Generally, the measurement box used for the measurement of the qubits, is called a Z-basis measurement, i.e., after applying it on a qubit, it will result either $∣0⟩$ or $∣1⟩$ state. For X-basis measurement, we put a Hadamard gate before the Z-basis measurement box, and for Y-basis measurement, we need to put $S_{†}$ and Hadamard gates consecutively before the Z-basis measurement box. Let us explain the reason for the above operations so as to make them different measurement bases. It can be observed that, X-basis states ${∣+⟩,∣−⟩}$ map to Z-basis states ${∣0⟩,∣1⟩}$ by an operation of Hadamard gate, hence if after measurement, we get $∣0⟩$ or $∣1⟩$, that correspond to $∣+⟩$ or $∣−⟩$ states respectively before the Hadamard operation. Similarly, Y-basis states ${∣l⟩,∣r⟩}$ can be mapped to Z-basis states ${∣0⟩,∣1⟩}$, by applying $S_{†}$ and Hadamard gates consecutively.

## Quantum circuit for different measurement bases

For Z-basis measurement:

from qiskit import QuantumCircuit, execute from strangeworks.qiskit import get_backend #taking one classical c[0] and one quantum q[0] register qc = QuantumCircuit(1, 1) #measuring the q[0] qubit in Z-basis and storing the results in c[0] bit qc.measure([0], [0]) # get a Strangeworks backend backend = get_backend("qasm_simulator") # execute circuit execute(qc, backend, shots=100)

After running the codes, $∣0⟩$ results with 100% probability, as initially the qubit was in$∣0⟩$ state, and it was measured in Z-basis state.

For X-basis measurement:

from qiskit import QuantumCircuit, execute from strangeworks.qiskit import get_backend #taking one classical c[0] and one quantum q[0] register qc = QuantumCircuit(1, 1) #measuring the q[0] qubit in X-basis and storing the results in c[0] bit qc.h(0) qc.measure([0], [0]) # get a Strangeworks backend backend = get_backend("qasm_simulator") # execute circuit execute(qc, backend, shots=100)

Initially, the qubit was in $∣0⟩$ state, after measuring it in X-basis, $∣0⟩$ and $∣1⟩$ results with equal probability. As $∣0⟩$ and $∣1⟩$ correspond to $∣+⟩$ and $∣−⟩$ states respectively, that means the state before the Hadamard operation was in equal superposition of $∣+⟩$ and $∣−⟩$ states. This can be understood, as $∣0⟩$ can be written as equal superposition of $∣+⟩$ and $∣−⟩$ states, $∣0⟩=2 ∣+⟩+∣−⟩ $.

For Y-basis measurement:

from qiskit import QuantumCircuit, execute from strangeworks.qiskit import get_backend #taking one classical c[0] and one quantum q[0] register qc = QuantumCircuit(1, 1) #measuring the q[0] qubit in Y-basis and storing the results in c[0] bit qc.sdg(0) qc.h(0) qc.measure([0], [0]) # get a Strangeworks backend backend = get_backend("qasm_simulator") # execute circuit execute(qc, backend, shots=100)

In a similar way, in Y-basis measurement, $∣0⟩$ and $∣1⟩$ result with equal probabilites, meaning that, $∣l⟩$ and $∣r⟩$ states were there before the Y-basis measurement. As it can be seen that $∣0⟩$ can be written as equal superposition of $∣l⟩$ and $∣r⟩$ states, i.e., $∣0⟩=2 ∣l⟩+∣r⟩ $

# Basic single-qubit gates

Basic single-qubit gates are Hadamard ($H$), $X$, $Y$, $Z$, $S$, $S_{†}$, $T$, $T_{†}$, $U_{1}(ϕ)$, $U_{3}(θ,ϕ,λ)$. The matrix form of the above are given in the following.

$H=2 1 [11 1−1 ]$, $X=[01 10 ]$, $Y=[0i −i0 ]$, $Z=[11 0−1 ]$, $S=[10 0i ]$, $S_{†}=[10 0−i ]$, $T=[10 0e_{iπ/4} ]$, $T_{†}=[10 0e_{−iπ/4} ]$, $U_{1}(ϕ)=[10 0e_{iϕ} ]$, $U_{3}(θ,ϕ,λ)=[cos(θ/2)e_{iϕ}sin(θ/2) −e_{iλ}sin(θ/2)e_{i(ϕ+λ)}cos(θ/2) ]$

It can be noted that $U_{3}$ gate is the general form, i.e., any single-qubit gates, can be written in terms of $U_{3}$ gate by choosing proper values, of $θ$, $ϕ$ and $λ$. $U_{1}$ gate in general called as phase gate, i.e., any value of phase gate can be achieved with this gate.

Let us some of single-qubit gate operations on the quatum states,

A Hadamard gate acting on computational basis states, ${∣0⟩,∣1⟩}$ gives the following X-basis states,

$H∣0⟩→∣+⟩$

$H∣1⟩→∣−⟩$

Again, applying Hadamard gate on ${∣+⟩,∣−⟩}$ gives computational basis states ${∣0⟩,∣1⟩}$

$H∣+⟩→∣0⟩$

$H∣−⟩→∣1⟩$

Similarly, Pauli ${X,Y,Z}$ operations can be given as,

$X∣0⟩→∣1⟩$

$X∣1⟩→∣0⟩$

$Y∣0⟩→i∣1⟩$

$Y∣1⟩→−i∣1⟩$

$Z∣0⟩→∣0⟩$

$Z∣1⟩→−∣1⟩$

Application of phase gates and general unitary operations can be given as,

$U_{1}(ϕ)∣0⟩→∣0⟩$

$U_{1}(ϕ)∣1⟩→e_{iϕ}∣1⟩$

$U_{3}(θ,ϕ,λ)∣0⟩→cos(θ/2)∣0⟩+e_{iϕ}sin(θ/2)∣1⟩$

$U_{3}(θ,ϕ,λ)∣1⟩→−e_{iλ}sin(θ/2)∣0⟩+e_{i((ϕ+λ)}cos(θ)∣1⟩$

# Basic two-qubit gates

Controlled-Not gate (CNOT) is a two-qubit gate, also called as an entangling gate, is used for creating entanglement between two or more than two qubits. CNOT acts on two qubits, where one is control and the other qubit is the target qubit. In a CNOT operation, Not gate is applied on the target qubit when the control qubit is in $∣1⟩$ state. The matrix form of CNOT operation can be given as,

$CNOT=⎣⎢⎢⎢⎡ 1000 0100 0001 0010 ⎦⎥⎥⎥⎤ $. There are other two-qubit basic gates such as controlled-$U_{3}$ gate, and cotrolled-$U_{1}$ gates, whose forms can be given as follows,

$Controlled−U_{3}(θ,ϕ,λ)=⎣⎢⎢⎢⎡ 1000 0100 00cos(θ/2)e_{iϕ}sin(θ/2) 00−e_{iλ}sin(θ/2)e_{i(ϕ+λ)}cos(θ/2) ⎦⎥⎥⎥⎤ $,

$Controlled−U_{1}(ϕ)=⎣⎢⎢⎢⎡ 1000 0100 0010 000e_{iϕ} ⎦⎥⎥⎥⎤ $,

It can be noted that any two-qubit controlled-U operator can be designed using Controlled-$U_{3}$ gate, after choosing appropriate parameters.

Application of CNOT operation on the computational basis states can be shown as,

$CNOT∣00⟩→∣00⟩$

$CNOT∣01⟩→∣01⟩$

$CNOT∣10⟩→∣11⟩$

$CNOT∣11⟩→∣10⟩$

It can be observed that, in the last two cases, when the first qubits are in $∣1⟩$ state, then not gates are applied on the corresponding target qubits.

A control-U3 operation also can be shown as,

$Controlled−U_{3}(θ,ϕ,λ)∣00⟩→∣00⟩$

$C−U3(θ,ϕ,λ)∣01⟩→∣01⟩$

$C−U3(θ,ϕ,λ)∣10⟩→∣1⟩(cos(θ/2)∣0⟩+e_{iϕ}sin(θ/2)∣1⟩)$

$C−U3(θ,ϕ,λ)∣11⟩→∣1⟩(−e_{iλ}sin(θ/2)∣0⟩+e_{i(ϕ+λ)}cos(θ/2)∣1⟩)$

# Other two-qubit and multiple qubit gates

Swap gate is another two-qubit important gate which just swaps the qubit states. The matrix form of the swap gate is given as,

$Swap=⎣⎢⎢⎢⎡ 1000 0010 0100 0001 ⎦⎥⎥⎥⎤ $

This can be designed using three CNOT gates, such as $Swap_{01}=CNOT_{01}CNOT_{10}CNOT_{01}$, where $Swap_{01}$

denotes Swap gate is applied on the qubits $q[0]$ and $q[1]$, and $CNOT_{ij}$ denotes the CNOT gate is applied on $i_{th}$ and $j_{th}$ qubits, where $q[i]$ acts as the control qubit and $q[j]$ acts as the target qubit.

Demonstration of Swap gate:

from qiskit import QuantumCircuit, execute from strangeworks.qiskit import get_backend #taking two classical c[0,1] and two quantum q[0,1] registers qc = QuantumCircuit(2, 2) #preparing 10 state qc.x(0) #applying Swap operation on q[0] and q[1] qubits qc.cx(0,1) qc.cx(1,0) qc.cx(0,1) #now after the swap operation the state becomes 01 qc.measure([0,1], [0,1]) # get a Strangeworks backend backend = get_backend("qasm_simulator") # execute circuit execute(qc, backend, shots=100)

It can be observed that, initially the qubits were in $∣10⟩$ state, after the swap operation, it was converted to $∣01⟩$ state. Similarly, for other two-qubit states, it can be checked.

There is another two-qubit anti-controlled operation, which acts on two qubits, among which one is anti-control qubit, and the other is target qubit. In an anti-control-U operation, U is operated on the target qubit, when the control qubit is in $∣0⟩$ state.

Let us take an example of anit-control Not operation.

from qiskit import QuantumCircuit, execute from strangeworks.qiskit import get_backend #taking two classical c[0,1] and two quantum q[0,1] registers qc = QuantumCircuit(2, 2) #preparing 01 state qc.x(1) #applying anti-control Not operation on q[0] and q[1] qubits, where q[0] is the control qubit and q[1] is the target qubit qc.x(0) qc.cx(0,1) qc.x(0) #now after the anti-control Not operation the state becomes 00 qc.measure([0,1], [0,1]) # get a Strangeworks backend backend = get_backend("qasm_simulator") # execute circuit execute(qc, backend, shots=100)

As can be seen from the above measurement, initiallly the qubits were in $∣01⟩$ state, after the application of anti-control Not operation, the state becomes $∣00⟩$ state.

Let us now discuss a three-qubit gate named Toffoli gate, which is also known as control-control-Not gate. Toffoli gate is applied on a three-qubit system, where the first two qubits act as the control qubit and the third qubit acts as the target qubit. When the control qubits are in $∣1⟩$ state, then Not gate is applied on the target qubit.

A circuit demonstration for one of the examples is given here,

from qiskit import QuantumCircuit, execute from strangeworks.qiskit import get_backend #taking two classical c[0,1,2] and two quantum q[0,1,2] registers qc = QuantumCircuit(3, 3) #preparing 110 state qc.x(0) qc.x(1) #applying Toffoli operation on q[0], q[1] and q[2] qubits qc.ccx(0,1,2) #now after the Toffoli operation the state becomes 111 qc.measure([0,1,2], [0,1,2]) # get a Strangeworks backend backend = get_backend("qasm_simulator") # execute circuit execute(qc, backend, shots=100)

It can be observed that, applying Toffoli operation on $∣110⟩$ state, it converts to $∣111⟩$. Similarly, for other cases, it can be checked by applying Toffoli gate onto different states.