A Busy Developer’s Guide to Quantum Computing - Part 3

Mariusz Krzemień

22 Apr 2024.7 minutes read

A Busy Developer’s Guide to Quantum Computing - Part 3 webp image

In the previous Parts of the series I gave a quick overview of a quantum computer and talked a little bit about qubits and other building blocks behind that picture.

Check all the articles from the series:

In this Part I’m going to show you how programming a quantum computer can look in practice. (Following examples and visuals were prepared using IBM Qiskit, but they are quite universal.)

Programming a quantum computer

In the previous Parts I described how a computation on a quantum computer looks like. In short, we start by preparing a quantum register large enough to fit all our inputs. We initialize the register by putting it in a superposition — with inputs represented by basic states. Then we apply some transformations — each transformation handling all inputs in parallel. Finally, we make a measurement and copy the result to classic memory.

Of course, the transformations will be specific to the problem at hand. So we need a way to define custom transformations.

Not all transformations are possible, though. In Part 1, where I talked about vectorization, I mentioned that a quantum computer imposes certain restrictions:

  • a transaction must not branch out — when applied to a register in superposition, it applies the same function to all basic states (all inputs),
  • and the function needs to be reversible.

Intuitively, “reversibility” means that for each input the function has a unique output and vice-versa. In general, it is hard to prove that a transformation is reversible without resorting to algebra. Fortunately, we can avoid the problem by building custom transformations using existing ones.

Quantum gates

We can build complex transformations using a palette of well-defined simple operations called gates. Many of them resemble logic gates known from a classic computer.

IBM Qiskit provides a palette of standard gates. The palette is universal — any other operation can be assembled from them. And — because any combination of reversible operations is also reversible — we are guaranteed that these custom operations will be correct.

A gate can be defined as a function on basic states. Here are some gates operating on a single qubit. I will be using them in following examples.


Definition of gates I (Identity) and X (Not)

I gate (Identity) leaves the qubit unchanged. Later, I’m going to use it as a placeholder to signify that the qubit is not transformed.

X gate (logical Not) flips a bit value of the qubit.

The concept of phase, introduced in Part 2, allows to define more operations on a single qubit, that have no analogy in a classic computer.


Definition of gates Z and Y

Z gate switches the phase of the basic state |1⟩ and besides that leaves the qubit unchanged.

Y gate is a combination of Z and X.

A transformation on a basic state can result in a superposition. With that, even more gates are possible.


Definition of H gate (coefficient p represents a probability distributed evenly)

The above gate is called H (Hadamard’s gate). It is commonly used to initialize a qubit in a superposition with probabilities distributed evenly.

Other gates returning superpositions with different probabilities are also possible. In fact, there’s an infinite number of different possible gates even for a single qubit.

Let’s also see one definition of a gate spanning two qubits:


Definition of CX gate (with “control” on the left and “target” on the right)

The above gate is called CX (Controlled-Not). It can be interpreted as a conditional Not operation. Here, the left qubit plays a role of “control” and the right qubit is a “target”. Input to control qubit decides if the target qubit will be flipped or not.

Quantum circuits

A number of quantum gates operating on qubits form a quantum circuit.


An example quantum circuit

Each horizontal line represents one qubit evolving in time. Gates are applied to qubits in sequence, transforming their states. Gates cover a single qubit or span multiple qubits.

This particular circuit puts qubits in an entangled state (coefficients omitted for simplicity):


The resulting entangled state

In the above example:

  • there are two qubits q0 and q1 and two classic bits c0 and c1 (classic bits are collapsed)
  • gate H (represented here as a box with letter “H”) is applied only to q0,
  • gate CX (represented here as a vertical line with a “plus”) spans both q0 and q1,
  • then both q0 and q1 are measured and the measurement results are copied to c0 and c1, respectively.

In a moment, I’m going to show in detail why this quantum circuit produces the entangled state. Meanwhile, we can run an experiment to prove that the resulting state is in fact entangled.


A result of an experiment with a quantum circuit with H and CX gate (2000 shots)

Now, let’s use gate definitions shown previously to analyze the above circuit.

Analyzing a quantum circuit

At the beginning, both qubits q0 and q1 are initialized to |0⟩. So we start with the initial state |00⟩.

Then we apply the gates in sequence:

  • Applying an H gate to the qubit q0 results in a superposition: (|00⟩ + |10⟩).
  • Applying a CX to |00⟩ will give an unchanged: |00⟩.
  • Applying a CX to |10⟩ will flip the target qubit: |11⟩.
  • Applying a CX to the whole superposition results in a superposition with preserved probabilities, but with one of the basic states flipped: (|00⟩ + |11⟩). It is the expected entangled state.

In the above circuit the transformations of individual basic states can be analyzed independently. Sometimes it’s not the case as you will see in the following example.

Transforming multiple states at once

As a simple example, let’s look at a quantum circuit with one qubit and two H gates:

An example quantum circuit with two H gates

If the qubit is initialized to 0 and we apply the gates in sequence:

  • The first H gate will transform the qubit state to (|0⟩ + |1⟩).
  • The second H gate will transform |0⟩ to (|0⟩ + |1⟩) and |1⟩ to (|0⟩ - |1⟩).
  • Each of these states — if measured independently — would be indistinguishable. But when combined in a resulting superposition, the two |1⟩s cancel each other out leaving only |0⟩ as a result.

If we run an experiment, we’ll get:


A result of an experiment with a quantum circuit with two H (qubit initialized to 0)

Similarly, if the qubit is initialized to 1:

  • The first H gate will transform the qubit state to (|0⟩ - |1⟩).
  • The second H gate will preserve the flipped phase for the |1⟩ and transform |0⟩ to (|0⟩ + |1⟩) and ( -|1⟩) to (-(|0⟩ - |1⟩)) which can be simplified to (-|0⟩ + |1⟩).
  • Again — if measured independently — each of these states would be indistinguishable. But when combined in a resulting superposition, the two |0⟩s cancel each other out leaving only |1⟩ as a result.

If we run an experiment, this time we’ll get:


A result of an experiment with a quantum circuit with two H (qubit initialized to 1)

This is a good example showing how parallel computation of different basic states can affect each other in the result.

Let’s briefly discuss two more concepts before jumping to the running example.

Ancilla qubits

As another example, here is a quantum circuit with a custom operation spanning all qubits.


An example of a quantum circuit with the StubFunction

We are going to use this operation later in Part 4 as a test stub representing some search problem. The function verifies if a given input is equal to some hidden “password”. Here, I’d like to specifically highlight the role of ancilla qubits.

Not all qubits are treated equally by the operation. The first three qubits represent proper inputs. They are expected to be initialized to all bitwise combinations with H gate.


Example bits of input plus a single ancilla bit for storing intermediate result

The fourth qubit is called ancilla. In general, there may be more than one. It is reserved to store intermediate results of the computation. The ancilla qubit is expected to be initialized to |0⟩.

In this example, during computation the ancilla qubit gets entangled with input qubits. It allows the operation to associate each input with its individual results, thus enabling for parallel computation. I explained this in more detail in Part 2 where I talked about entanglement.

Finally, the ancilla gets disentangled returning back to its initial state |0⟩.

Encoding information in gates

When defining an operation, take in mind that not all input information has to be passed to it in qubits. A significant part of the problem will be encoded in its internal structure as gates.

In the case of the StubFunction above, the gates structure encodes some hidden “password”.

In the case of TSP for example, input qubit values might be used just to identify a particular route. All remaining information required to calculate the length of a route could be encoded as gates.


In this Part of the series I introduced the concept of quantum gates and circuits. I described how to use gates to build complex transformations. I also discussed a role of ancilla qubits and briefly mentioned encoding information in gates.

In Part 4 I’m going to show all these elements in a running example.

Reviewed by Paweł Stawicki, Bartłomiej Turos

Check all the articles from the series:

Blog Comments powered by Disqus.