# What Is a Quantum Circuit?

A quantum circuit is a series of quantum gates that operate on qubits to perform a quantum algorithm.

## Building Blocks of Quantum Circuits

Common building blocks and terms associated with quantum circuits include:

• Qubit: A qubit, short for “quantum bit,” is the fundamental unit of quantum information, just like a bit in classical computing. Qubits can exist in a state of $$0$$ and $$1$$ simultaneously, known as superposition. In contrast, a bit can store a single value of either $$0$$ or $$1$$.
• Qubit initialization: When defining a quantum circuit, all the qubits in the quantum circuit start in the $$|0⟩$$ state by default. By initializing the qubit, you can set the qubit to a known state before the computation begins.
• Quantum gates: Quantum gates are building blocks of quantum circuits that perform operations on single or multiple qubits:
• Single-qubit gates are operations that affect individual qubits, such as the Pauli X, Y, and Z gates; Hadamard gate; and phase gates. They can change the state of a qubit by rotating it around different axes on the Bloch sphere.
• Multi-qubit gates are operations on two or more qubits, such as the controlled X and controlled Z gates, which produce entanglement, and swap gates. Entanglement is a uniquely quantum computational characteristic that allows qubits to be correlated in ways not possible with classical bits.
• Measurement: The process of extracting information from qubits of a quantum circuit execution is called measurement. Measurements collapse the qubit’s state to either $$|0⟩$$ or $$|1⟩$$ with associated probabilities.

## The Difference Between Classical and Quantum Circuits

A classical circuit uses logic gates, such as AND, OR, NOT, NAND, NOR, XOR, and XNOR, to perform binary operations on classical bits to produce a desired output. By contrast, a quantum circuit modifies the information held in qubits through operations of quantum gates. The core attribute of quantum circuits lies in their ability to exhibit quantum computing characteristics such as quantum parallelism, interference, entanglement, and superposition. Quantum algorithms leverage these characteristics in the quantum circuits to perform computations that can be more efficient than their classical counterparts for certain applications.

## Quantum Circuit Workflow with MATLAB

MATLAB® provides capabilities to build quantum circuits using built-in and customizable composite gates. Common steps in the development of a quantum algorithm using a quantum circuit are building, simulating, and executing the circuits on hardware.

MATLAB workflow for quantum circuit development.

### Building the Quantum Circuit

As an example, we create the Greenberger-Horne-Zellinger (GHZ) state, which is an entangled quantum state for three qubits. The GHZ state is crucial in quantum computing as it represents a maximally entangled state of multiple qubits to test the principles of quantum mechanics.

To create a three-qubit GHZ state, define all the gates before calling the quantumCircuit function:

gates = [hGate(1);cxGate(1,2);cxGate(2,3)];
circ = quantumCircuit(gates, Name = "circuit");
plot(circ)


Quantum circuit for creating a three-qubit GHZ state.

This GHZ quantum circuit uses two distinct types of quantum gates:

• The first gate is the Hadamard gate, which operates on a single qubit to convert its basis states into a superposition state where the probabilities for measuring either state are equal. This gate is required for generating quantum superpositions and is mathematically depicted as$\frac{1}{\sqrt 2}\begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix}$
• The second gate is the controlled NOT (CNOT) gate, a two-qubit quantum gate that conditionally flips the state of the second (target) qubit when the first (control) qubit is in the $$|1⟩$$ state. The CNOT here is establishing quantum entanglement between the two qubits.

To understand what is happening behind the quantum circuit, it’s helpful to break down its operation into four distinct phases, examining the state of the system sequentially before and after each gate is applied.

The quantum circuit for creating a GHZ state, with vertical dotted lines (barriers) and horizontal index depicting the various phases throughout the circuit’s progression and the vertical index depicting the qubit index.

At phase 0, all qubits begin in the initialized state of $$|0⟩$$, making the overall state of the quantum circuit $$|0⟩^{\oplus3}$$.

During phase 1, the Hadamard gate acts on the first qubit, which results in:

$$H*|0⟩ |00⟩ = \frac{1}{\sqrt 2} [|0⟩+|1⟩]|0⟩^{\oplus 2} = \frac{1}{\sqrt 2} [|000⟩+|100⟩]$$

Here, $$H$$ represents the Hadamard transformation, which can be expressed as:

$$H = \frac{1}{\sqrt 2} \begin{bmatrix} 1 & 1 \\ 1 & -1 \end{bmatrix} \begin{matrix} 1 \\ 0 \end{matrix} = \frac{1}{\sqrt 2} \begin{matrix} 1 \\ 1 \end{matrix} = \frac{1}{\sqrt 2} \begin{matrix} 1 \\ 0 \end{matrix} + \frac{1}{\sqrt 2} \begin{matrix} 0 \\ 1 \end{matrix} = \frac{1}{\sqrt 2} [|0⟩ + |1⟩]$$, where the matrix form of $$|0⟩$$ is $$\begin{matrix} 1 \\ 0 \end{matrix}$$ and $$|1⟩$$ is $$\begin{matrix} 0 \\ 1 \end{matrix}$$.

At phase 2, the CNOT gate is applied between the first qubit (control) and the second qubit (target), yielding:

$$CNOT* \frac{1}{\sqrt 2} [|000⟩ + |100⟩ ] = \frac{1}{\sqrt 2} [|000⟩ + |110⟩ ]$$

In phase 3, another CNOT gate is applied, this time between the second qubit (control) and the third qubit (target), resulting in:

$$CNOT* \frac{1}{\sqrt 2} [|000⟩ + |110⟩ ] = \frac{1}{\sqrt 2} [|000⟩ + |111⟩ ]$$

The process just described is an interpretation of a quantum circuit that constructs a three-qubit GHZ state.

### Simulating Quantum Circuits

Building quantum circuits requires iteration on the circuit design to refine quantum gates and confirm behavior. Before you run your quantum circuits on a QPU, which can be time-consuming and costly, you can experiment on your quantum circuits by doing simulation locally. The simulate function in MATLAB enables you to simulate a quantum circuit and find the final state after running the quantum circuit.

### Executing Quantum Circuits

You can connect your quantum circuits directly to remote quantum hardware devices and simulators that are provided by quantum computing services from Amazon® Web Services (AWS®) and IBM® Qiskit® Runtime Services.

## Quantum Circuit Visualization with MATLAB

You can use common tools such as plotting, matrices, and tables to visualize individual qubits after measurement of the quantum circuit.

### Bloch Sphere

The Bloch sphere is a representation of the state space of a single qubit in quantum mechanics, using the surface of a sphere. Each point on the sphere’s surface corresponds to a possible qubit state, with the poles representing the states $$|0⟩$$ and $$|1⟩$$, and any other point representing a superposition of these states. You can use it to visualize qubit states and the effects of quantum gates in a geometric model.

A Bloch sphere representing a quantum state |0⟩.

### Quantum State

A quantum state is a mathematical representation of the condition of a quantum system, encapsulating all the information necessary to describe its physical properties. A quantum state can be formulated in terms of linear algebra as a state vector in a complex vector space, known as the Hilbert space. The state vector evolves according to the rules of quantum mechanics, allowing for phenomena such as superposition and entanglement. For a quantum computer, the state vector encapsulates the probabilities and phases of all possible states of the qubits, providing a complete description of the system’s quantum state at any given moment.

The following table shows the output of the table() function that returns the possible quantum states, amplitudes, and probabilities for a three-qubit GHZ quantum circuit.

T = table(S.BasisStates, S.Amplitudes, S.Amplitudes.^2, VariableNames=["Basis States", ...
"Amplitudes","Probabilities=Amplitudes**2"])

T = 8x3 table

 Basis States Amplitudes Probabilities=Amplitudes**2 1 "000" 0.7071 0.5000 2 "001" 0 0 3 "010" 0 0 4 "011" 0 0 5 "100" 0 0 6 "101" 0 0 7 "110" 0 0 8 "111" 0.7071 0.5000

The state vector for the GHZ state can be written as $$= 0.7071*|000⟩ + 0.7071*|111⟩$$.

### Unitary Matrix Representation

A unitary matrix is a complex square matrix that, when multiplied by its conjugate transpose, results in the identity matrix. This property ensures that the matrix preserves the norm of vectors upon transformation, which is a fundamental requirement in quantum mechanics for the evolution of quantum states, as it reflects the principle of conservation of probability. Every quantum gate and a combination of the gates in a form of quantum circuit that act on multiple qubits can be represented as a unitary matrix.

The GHZ quantum circuit’s unitary matrix representation is as follows:

getMatrix(circ)

ans = 8x8

0.7071        0        0        0    0.7071         0         0         0

0   0.7071        0        0         0    0.7071         0         0

0        0   0.7071        0         0         0    0.7071         0

0        0        0   0.7071         0         0         0    0.7071

0        0        0   0.7071         0         0         0   -0.7071

0        0   0.7071        0         0         0   -0.7071         0

0   0.7071        0        0         0   -0.7071         0         0

0.7071        0        0        0   -0.7071         0         0         0


### Histogram

Let’s say we want to run a quantum circuit with 1,000 shots. We can tabulate and summarize the experiment results in a table and plot the results in a histogram.

% inputs to the function below are the state and the number of shots
M = randsample(s,1000);
T = table(M.MeasuredStates, M.Counts, M.Probabilities, ...
VariableNames=["States","Counts","Probabilities”])

T = 2x3 table

 States Counts Probabilities 1 "000" 476 0.476 2 "111" 524 0.524
% input to the histogram function is the state
histogram(s)


The histogram shows the approximately equal probability distribution of '000' and '111' states from 1,000 shots of a GHZ circuit, illustrating the expected maximally entangled state of three qubits.

See examples of using quantum circuits in machine learning, finance, optimization, and chemistry.