Photo by Phil Hearing

QC — Programming with Quantum Gates (Single Qubits)

Quantum computing uses a different paradigm in writing programs. In this article, we describe the quantum gates — the basic units for quantum programming. If you are not familiar with Qubits, we will suggest you visit Part2 and Part 3 of this series first.

The diagram above is a single-bit adder on a classical computer. It is a bitwise a + b addition with the carry c. In quantum computing, we apply quantum gates U to manipulate a superposition (qubits).

The programming paradigm for quantum computing is similar to a classical circuit. A program can be written as a diagram with a sequence of the quantum gates (a quantum circuit).

In classical computing, the number of input and output can be different.

This is forbidden in quantum computing. For operators to be reversible in both directions, no information can be lost. We cannot implement a quantum operator in a quantum computer if the operation is not reversible. Therefore, both the input and output must have the same number of qubits. Following similar logic, qubits do not branch out or merge together. We don’t have the nicely if-then-else or while statements. That is why things can get complicated while it looks obvious for classical computing.

Qubits manipulation

A superposition can be written as:

Therefore, quantum computing can be visualized as manipulating θ and Φ of the state vector and making measurements.

Quantum algorithm

So what is the general flow of a quantum algorithm? First, we prepare the superposition. Then we encode the problem information into the superposition and manipulate it in a high dimensional space. Finally, we apply interference to consolidate the superposition into fewer outcomes. (Note: there are other possibilities and omitted for simplicity.)

Source

For example, we can prepare three qubits to represent all possible computational basis evenly.

We further encode the problem information into the superposition and manipulate it. Then we make measurements. The following diagram contains 1000 runs for the Deutsch-Jozsa Algorithm and displays the chance of measuring the qubits with a specific value.

Source: IBM

Without getting into the details, the function we want to investigate has the most common measurement |00⟩. Below is another pattern for a different function. Quantum algorithms will use these measurements to derive their results. In this example, different patterns conclude different types of functions that we are studying.

So let’s study the quantum gates one-by-one.

Hadamard Gate

One of the most common quantum gates is the Hadamard Gate.

This is the corresponding Dirac notation.

This gate prepares an equal superposition of all computational bases. For example, when we apply it to the ground state |0⟩, it transforms into

If it is measured, there is an equal chance to be measured as |0⟩ or |1⟩. Below is the superpositions after applying the Hadamard gate to |0⟩ and |1⟩.

If we apply the gate again, the qubit restores to its original value.

Source

So Hadamard gate is more than preparing a superposition. It can be used to consolidate the superposition also.

For an n-qubit system, we can apply n Hadamard gate in parallel to produce an evenly distributed superposition.

Here is the corresponding equation for the superposition:

Global phase

Actually, a superposition can be represented as

Two vectors can be mathematically different. But if it is only by a global phase, it is indistinguishable in the real world under any measurements. For example, two vectors below are different by a global phase only (Δγ = π). As long as no operators or same operators are applied to both states, they have the same measurements.

This is different when they have different relative phase. Both vectors below are indistinguishable when measured. But after applying a Hadamard gate, they transform to |0⟩ and |1⟩ separately.

Some algorithms can take advantage of this to consolidate vectors into different groups and each group will have a different measurement.

Pauli gates

Other single-qubit gates are pretty straightforward. So feel free to browse through the single-gate quickly. The next 3 Pauli gates rotate the superposition along x, y or z-axis.

X-gate

The X-gate switches the amplitudes of the |0⟩ and |1⟩ basis. It rotates the vector along the x-axis. It is often described as the classical NOT gate because it flips |0⟩ to |1⟩ (or vice versa).

i.e.

Y-gate

Y-gate rotates around the y-axis.

It switches the amplitudes and multiple them with ±i. The results of applying X-gate and Y-gate is different by a phase only.

Z-gate

The Z-gate introduces a -1 phase into the |1⟩ basis but keeps |0⟩ the same. It rotates a single qubit by π around the Z-axis.

i.e.

Phase shift

There are a few gates that rotate the vector around the z-axis, namely Z, S, and T-gate.

We just cover the Z-gate that rotates Φ by π.

S-gate

S-gate changes the phase of |1⟩ by a factor of i. i.e. a π/2 rotation around the Z-axis.

T-gate

T-gate has a π/4 rotation around the Z-axis.

Phase shift gate

The generic phase shift gate rotates z by an arbitrary θ. It changes the phase of |1⟩.

Measurement

Here is the symbol in taking a measurement.

Identity matrix

An identity gate does nothing.

But it is often included inside equations for clarity.

Next

In general, all these single-qubit operators is about moving the vector along the surface of the unit sphere. Let’s move to multi-qubit gates which are more tricky.