Quantum Programming

# Introduction to Quantum Programming

My initial dive into the world of quantum programming

## Why Quantum Programming?

I found Qiskit (by IBM) which is both a library/API for interacting with quantum primaries as well as having a textbook/intro course that teaches the fundamentals of Quantum Programming: qiskit.org/learn. This is where the images have been sourced from.

This is really just a recording of my understanding based on that textbook. I would definitely recommend you go there to work through it yourself as many concepts take some work to really understand and they have exercises you can work through that are very good.

## Understanding quantum states

The first stage was really to understand quantum states - these start off seeming to be binary strings like 01 or 11011001. This is the same as classical computing but quickly it is explained that there are more complicated states.

So there are still bit strings for our outputs, this is when the quantum state is measured back into classical bits to get a solution. But for the intermediate quantum states we need to talk about amplitudes.

### Amplitudes

Amplitudes are basically complicated probabilities - massively oversimplified they have an amplitude (size) and a phase (direction). In general they are represented as vectors with numbers ranging from -1 to 1. The important thing to remember about amplitudes is that they only become probabilities when you square the final amplitude, so the phase matters during quantum operations and combinations but disappear when measuring the final state.

### State vectors

Quantum state end up being represented as vectors for a one qubit state you have a 2 vector with each entry being the amplitude of that binary state usually represented as -1, 0 or 1 but with some exceptions. The amplitude of the whole vector is usually (I think always but not totally sure) 1, ie. making it a unit vector, so it is common to have some factor in front of the vector to scale it appropriately.

If you have a multi-qubit state then the vector will be 2*n where n is the number of qubits see below for an example of a 2 qubit state a.

(credit for this and all images in this blog qiskit.org/learn)

### Standard bases

The two bases for quantum states that were introduced are the 01 basis (this is the normal basis you would think of where each axis is a 0 state or a 1 state. The other basis is the \(\ket{+}\) and \(\ket{-}\) states, these are

Visually represented as:

The thing that is interesting about these states to me is that they are a superposition of 0 and 1 with the \(\ket{-}\) state inverting the phase of the 1 bit. So they have equal odds of ending up as either 0 or 1 if measured at that point but when combined with other qubits they can change the phase and that leads to interesting possibilities.

These are the bases for individual qubits, multiquibit states would use a combination of these.

### Multi-qubit states

There are basically two types of multi-qubit states, product states and entangled states. The basic difference is that a product state can be described by reference to each individual qubit. That is if we have 2 qubits one is \(\ket{+}\) and one is \(\ket{0}\) then we can call that \(\ket{+0}\) as the rightmost bit stays as 0 and the first bit is in a superposition. If that is not possible (ie the qubits are not separable) then the state is entangled.

The right state is entangled as you can’t split the qubits up in any way into a product state.

## Understanding quantum gates

Quantum gates are basically transformations over qubits. There are several and there are some rules that govern what they can be. They are often represented by matrices, just as the states are often represented by vectors.

They must produce a new state that has total probability exactly 1, this basically means it must be reversible (ie. there is an inverse matrix that will reverse the operation), there is a note about remembering to reverse rotations so I think that rotations may be held somewhat separately.

This property of preserving total probability and being reversible is called unitary and the quantum gates are also referred to as unitary gates.

So what are these gates?

### Gates and bases

Gates are hard to classify in some sense, you have to think about them in reference to the measurement basis.

The 01 basis is referred to as the z basis (think of the z axis being vertical) and measurements to classical bits in this basis are z measurements. There are also x and y bases. The x basis is the +- basis we already mentioned. I am not totally sure what the y basis is as it is glossed over (I will come back and update this when I do find out!)

Either way measurements in the x basis are x measurements and no surprises, measurements in the y basis are y measurements. Each gate has to be thought about with reference to the bases.

#### X gate

So the x gate is the simplest. It flips a bit in the x basis, so if it started as a 0 it will become a 1. To be completely clear on this it actually flips the x component of a bit’s state. So if it’s in a superposition of 0 and 1 then the effect will depend on the magnitude of the x and y components. If they are equal (such as in the case of the \(\ket{+}\) or \(\ket{-}\) states) then the effect will be nothing. But if there is say a state b of [075, 0.25] in the z basis then x(b) = [0.25, 0.75].

#### H gate

A h (Hadamard) switches basis between z and x but it also has an effect on the phase. This makes a \(\ket{0}\) a \(\ket{+}\) and vice versa and a \(\ket{1}\) goes to a \(\ket{-}\) and vice versa.

#### Ry gate

The ry gate is a rotation around the y axis basically. That goes between the x and z states with some mixing depending on the angle. They use radians to specify the amount of rotation.

#### Cz gate

The cz gate is the most confusing to me. This is basically used to entangle qubits. It is a symmetric gate which means you don’t apply it to any individual qubit, you apply it to pairs of qubits. I was under the understanding that there were equivalents for more than 2 gates but I have found that in reality you can compose gates across multiple qubits by applying them in sequence. I am not sure of the rules governing these interactions however.

That covers the initial gates introduced in the intro course but there are definitely others!

## Application

The application covered is called Grovers search algorithm. This is where I started to understand how the particular properties of quantum bits, superposition etc are actually used to speed up certain computations.

I won’t go into much detail in this initial post but I will add another one soon with more details. I will just briefly outline the basic concept as a teaser/TLDR.

The problem is the 3-SAT problem, where you have a number of true/false expressions (bits/variables) and a number of clauses, the clauses are in CNF (Conjunctive Normal Form) and have a maximum of 3 bits - ie. two operations.

An Example: note. I am using programming literals not logic just for ease of typing. So | = OR ! = NOT and & = AND

(a | b | !c) & (b | e | f) & (a | !d | g) |

Its important to note that you can have many many clauses and many many bits or variables. What makes it 3-SAT is only 3 bits per clause.

The concept then is to think about the set of solutions (which are all bit strings that we will think of as vectors) as one axis and the set of non-solutions as another axis. These are orthogonal as no solution is contained in the non-solution set.

We start our vector as a superposition - so all bits have equal probability of 0 or 1. This will then be closer to the non-solution axis if there are more non-solutions than solutions which is probable.

Then we have what’s called an oracle, this has the key property that it inverts the phase of correct solutions. This has the effect of being a reflection around the non-solution axis. Then we need another reflection, this is called the diffuser. This reflects around the superposition vector. Because the angles of these reflections can be accumulated we can get closer to the set of correct answers.

This is the basic idea, thinking in vectors and applying operations that move the input vector closer to the direction of the solution.

Roger Milroy

blog content post post format