# Qubit, An Intuition #5 — Quantum Circuit and Reversible Transformation

*The beauty of Quantum Mechanics for Quantum Computation, featuring IBM Quantum*

**Andi Sama**** **— *CIO, **Sinergi Wahana Gemilang** with **Cahyati S. Sangaji*

`TL;DR; `

- Reversible operations in quantum circuits with examples and their implementation in IBM quantum.

- Discussion includes Reversible Transformations in Quantum Circuit, Quantum Circuit written as Unitary Matrix, and The Execution of the Quantum Circuit on IBM Quantum.

For an introductory helicopter view of the overall six articles in the series, please visit this link “Embarking on a Journey to Quantum Computing — Without Physics Degree.”

When we apply a set of unitary transformations U to a quantum state input|Ψ>, it produces|Ψ’> as the quantum-state output. Then, if we apply U conjugate transpose to|Ψ’>, we can get back our input |Ψ>. It means U is reversible as we can reconstruct the quantum-state input |Ψ> from the quantum-state output |Ψ’> by applying U conjugate transpose.

`Please refer to the previous articles:`

“*Qubit, An Intuition #1 — First Baby Steps in Exploring the Quantum World*” for a discussion on a single qubit as a computing unit for quantum computation.

- "*Qubit, An Intuition #2 - Inner Product, Outer Product, and Tensor Product*" for a discussion on two-qubits operations.

- "*Qubit, An Intuition #3 - Quantum Measurement, Full and Partial Qubits*" for examples on full and partial quantum measurements.

- "*Qubit, An Intuition #4 - Unitary Matrices for Quantum Computation**.*"

# Reversible Transformations in Quantum Circuit

## The Quantum Circuit and Reversed Quantum Circuit, An Example

The 3-qubits example below has |Ψ> = |010> and is transformed to |Ψ’> with a set of gate operations (U), resulting |Ψ’>. We do sequencing for the quantum circuit illustrated below from top to bottom (as ‘q₀q₁q₂’): q₀=0, q₁=1, q₂= 0. Note the X-gate operation on q₁ to set the initial state for qubit 1 to |1>.

Then, we feed |Ψ’> as input to U conjugate transpose. |Ψ’> is then transformed back to |Ψ> = |010> (the sequence is q₀ = Off, q₁ = On, q₂ = Off).

U is a quantum circuit consisting of a set of unitary gates. U conjugate transpose is the reversed quantum circuit, consisting of a set of the conjugate transposes of each unitary gate.

The conjugate transposes of H, CNOT, X, and Z gates are the same as the gate itself. Thus they stay at H, CNOT, X, and Z gates.

R90 is a 90 degrees (π/2) rotation along the X-axis. The R90 conjugate transpose (R90-inverse) indicates the conjugate transpose of R90.

## Simulating the Reversible Operations

We illustrate below the reversible operations in a quantum circuit (simulated with animation, with variable input, e.g., by the rotation along X-axis). The input |Ψ> becomes |Ψ’> after performing U, and the quantum state ouput|Ψ’> is back to |Ψ> after performing U conjugate transpose. The simulation is created by an online tool, Quirk (Strilanc, 2019).

# Quantum Circuit — Written as Unitary Matrix

Let’s focus on our quantum circuit at each stage S₁, S₂, and S₃. Ψ₁, Ψ₂, and Ψ₃ indicate the outputs at each state.

## Stage-1

Stage-1 is the tensor product of the H matrix on the first qubit, H matrix on the second qubit, and Identify matrix, producing an 8x8 matrix. The identity matrix represents the third qubit.

By doing the tensor product of each of the components by expanding their respective matrices for two Hs and one Identity, we get the following matrix (8x8 dimension) for stage-1.

## Stage-2

Stage-2 is the tensor product of the R90 matrix on the first qubit and CNOT matrix between the second dan third qubit, producing an 8x8 matrix. The identity matrix represents the third qubit.

By doing the tensor product of each component by expanding their respective matrices for R90 and CNOT, we get the following matrix (8x8 dimension) for stage-2.

## Stage-3

Stage-3 is the tensor product of the X matrix on the first qubit, Identity matrix on the second qubit, and Z matrix on the third qubit, producing an 8x8 matrix.

By doing the tensor product of each component by expanding their respective matrices for X, I, and Z, we get the following matrix (8x8 dimension) for stage-3.

## Combining Stage-1, Stage-2, and Stage-3

Finally, the combination of S₃ x S₂ x S₁ is as follows. The matrix multiplication of S₃ x S₂ x S₁ can be computed by S₃ x (S₂ x S₁) or (S₃ x S₂) x S₁, with the same result.

U = S₃ x S₂ x S₁, then intuitively, U conjugate transpose is (S₃ x S₂ x S₁) conjugated and transposed.

# Executing the Quantum Circuit on IBM Quantum

We are now using IBM Quantum Computer (IBM Quantum) on IBM Cloud to build our quantum circuit. Then, we execute our quantum circuit on one of the IBM Quantum computers, a 5-qubits IBM Quantum computer, ibmq_athens.

## The Quantum Circuit

First, we build the quantum circuit. Note that the barriers (white vertical dashes) and Identity gates (I-gates) do not affect quantum computation; they are there just for better visualization.

## Executing the Quantum Circuit in IBM Quantum

Then, execute our quantum circuit in ibmq_athens. The following is the result of the execution.

## Measurement and discussion

We get pure states ‘010’ (we read this in the sequence as ‘q₂q₁q₀’) after executing our quantum circuit with 8192 shots on IBM’s 5 qubits Quantum Computer “ibmq_athens.” 8192-shots means that the Quantum computer runs our quantum circuit 8192 times.

Measurement frequencies for states ‘010’ occurred 7695 times out of 8192 shots (93.9%), indicating the most likely answer that we are seeking.

We use a general U-gate in IBM Quantum (as part of our quantum circuit) to define R90 and R90 conjugate transpose gates. Note that this is a different U with the U in our example before (we defined U as the complete quantum circuit that is conjugate transposed to get |Ψ> back after transforming |Ψ> to |Ψ’> by performing U on |Ψ>).

A definition of a general U-gate in IBM Quantum is as follow:

We define R90 as follows:

We can also define R90 as rotation along X-axis using θ, ϕ, and λ by setting λ=π/2 and ϕ=-π/2. This R90 represents the x-axis rotation gate, Rₓ(θ).

Then R90 conjugate transpose can be expressed as:

## Auto-generated codes from the Quantum Circuit

Based on our quantum circuit, IBM Quantum generates QASM (quantum assembly) code automatically.

OPENQASM 2.0;

include "qelib1.inc";qreg q[3];

creg c[3];id q[0];

x q[1];

barrier q[0];

barrier q[1];

h q[0];

h q[1];

u(pi/2,-pi/2,pi/2) q[0];

cx q[1],q[2];

x q[0];

id q[1];

z q[2];

barrier q[0];

barrier q[1];

barrier q[2];

x q[0];

id q[1];

z q[2];

u(-pi/2,-pi/2,pi/2) q[0];

cx q[1],q[2];

h q[0];

h q[1];

id q[2];

barrier q[0];

barrier q[1];

barrier q[2];

measure q[0] -> c[0];

measure q[1] -> c[1];

measure q[2] -> c[2];

IBM Quantum also generates python code, utilizing IBM’s qiskit quantum programming framework.

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit

from numpy import piqreg_q = QuantumRegister(3, 'q')

creg_c = ClassicalRegister(3, 'c')

circuit = QuantumCircuit(qreg_q, creg_c)circuit.id(qreg_q[0])

circuit.x(qreg_q[1])

circuit.barrier(qreg_q[0])

circuit.barrier(qreg_q[1])

circuit.h(qreg_q[0])

circuit.h(qreg_q[1])

circuit.u(pi/2, -pi/2, pi/2, qreg_q[0])

circuit.cx(qreg_q[1], qreg_q[2])

circuit.x(qreg_q[0])

circuit.id(qreg_q[1])

circuit.z(qreg_q[2])

circuit.barrier(qreg_q[0])

circuit.barrier(qreg_q[1])

circuit.barrier(qreg_q[2])

circuit.x(qreg_q[0])

circuit.id(qreg_q[1])

circuit.z(qreg_q[2])

circuit.u(-pi/2, -pi/2, pi/2, qreg_q[0])

circuit.cx(qreg_q[1], qreg_q[2])

circuit.h(qreg_q[0])

circuit.h(qreg_q[1])

circuit.id(qreg_q[2])

circuit.barrier(qreg_q[0])

circuit.barrier(qreg_q[1])

circuit.barrier(qreg_q[2])

circuit.measure(qreg_q[0], creg_c[0])

circuit.measure(qreg_q[1], creg_c[1])

circuit.measure(qreg_q[2], creg_c[2])

# Moving Forward

Next in the final article “*Qubit, An Intuition #6**,* “ we will discuss two famous quantum algorithms, Shor’s and Grover Algorithms.

# References

- Advanced Maths, 2021, “
*Quantum Computing: Quantum Circuit Example*.” - Andi Sama, Cahyati S. Sangaji, 2021a, “
*Qubit, An Intuition #1 — First Baby Steps in Exploring the Quantum World**.*” - Andi Sama, Cahyati S. Sangaji, 2021b, “
*Qubit, An Intuition #2 — Inner Product, Outer Product, and Tensor Produc**t.*” - Andi Sama, Cahyati S. Sangaji, 2021c, “
*Qubit, An Intuition #3 — Quantum Measurement**.*” - Andi Sama, Cahyati S. Sangaji, 2021d, “
*Qubit, An Intuition #4 — Unitary Matrices for Quantum Computation**.*” - Andi Sama, 2021, “
*My Journey to Quantum Computing**.*” - IBM, 2021a, “
*IBM Quantum on IBM Cloud**.*” - IBM, 2021b, “Advanced single-qubit gates.”
- Strilanc, 2019, “
*Quirk — A drag-and-drop quantum circuit simulator.*”