Appendix C

Quantum Computing Experiments with MATLAB


 

 

C.2 COMPLEX NUMBERS AND MATRICES

We began this book by saying that complex numbers are fundamental for both quantum mechanics and quantum computing, so we are going to familiarize ourselves with the way they are dealt with in MATLAB.

To begin with, we need to declare complex number variables. This is easy: a complex has a real part and an imaginary part, both double. The imaginary part is declared by using the “i” or “j” character.1 For example, to declare the complex variable c = 5 + i, just type

image c = 5 + i

and the computer will respond with

c = 5.000 + 1.000i

 

Or, equivalently,

image c = 5 + j

c = 5.000 + 1.000i

 

Adding and multiplying complex numbers are entirely straightforward:

image d = 3 − 2i

d = 3 − 2i

 

image s = c + d

s = 8 + 3i

 

image p = c * d

p = 25 + 5i

 

There is also a handy complex conjugate:

image c1 = conj(c)

c1 = 5.000 − 1.000i

 

You may get the real and imaginary parts of a complex number by typing

image re = real(c)

re = 5

 

image im = imag(c)

im = 5

 

One can switch from Cartesian representation to polar representation:

image r = abs(c)

r = 7.0711

 

image angle(c)

ans = 0.78540.

 

And back from polar to Cartesian:

image c1 = r * exp (i*a)

c1 = 5.0000 + 5.0000i

 

It is extremely useful to plot a complex number. MATLAB has a lot of tools for mathematical visualization. We are going to present just one option here: the function compass:

image compass(re, im)

The computer will output a picture of a complex number as an arrow as in Figure 1.1 on page 16. The compass function plots the complex number as a vector springing from the origin. The function can take several complex vectors at once (refer to the online MathWorks documentation).

Our second ingredient is complex matrices. MATLAB is extremely powerful in dealing with matrices; indeed, the name MATLAB means MATrix LABoratory. What about vectors? Well, vectors are matrices.2 Nevertheless, first things first, so let us start with row and column vectors:

image bra = [1, 2 − i, 3i]

bra = 1 + 0i 2 − 1i 0 + 3i

 

image ket = bra′

1 + 0i

2 + 1i

0 + 3i

 

As you can see, the operator ′ on a complex matrix M is its complex conjugate (if the matrix is real, it is simply the transpose).

For the dot product (bra-ket), you just multiply them (there is, however, a dot( ) function):

image bra*ket

ans = 15

 

The norm function is built-in:

image norm(ket)

ans = 3.8730

 

So much for vectors. Let us declare a matrix, say, Hadamard:

image H = 1/ sqrt(2) * [11; 1 − 1]

H=

0.70711 0.70711

0.70711−0.70711

 

Now, we can calculate its inverse (as we found out already, H happens to be its own inverse):

image inv(H)

ans =

0.70711 0.70711

0.70711 −0.70711

 

MATLAB provides a trace function:

image trace(H)

ans = 0

 

The product is straightforward. (Caveat: If you make a dimensional mismatch, MATLAB will return an error!)

image H*I

ans =

0.70711 0.70711

0.70711−0.70711

 

We have met the tensor product several times throughout our text. Luckily, there is a primitive for it: it is called kron, as the tensor product of matrices is often referred to as the Kronecker product:

image kron(H, I)

ans =

0.70711 0.00000 0.70711 0.00000

0.00000 0.70711 0.00000 0.70711

0.70711 0.00000 − 0.70711−0.00000

0.00000 0.70711 − 0.00000 − 0.70711

 

We have used eigenvalues and eigenvectors throughout our text. How to compute them? Worry not! MATLAB comes to rescue. The command [E, V] =eig(M) returns two matrices: E, whose columns are the eigenvectors of M, and V, a diagonal matrix whose diagonal elements are the eigenvalues of M.

image [V, D] = eig(H)

V =

0.38268 −0.92388

−0.92388−0.38268

D=

−1 0

0 1

 

There is so much more to aid you in complex algebra. A quick Google search will showcase numerous tutorials on complex matrices manipulation. You can also find more by typing HELP at the prompt.

C.3 QUANTUM COMPUTATIONS

We are now ready for quantum computation. Here, we have two options: the first one is to implement step by step a quantum computer emulator, following the indications of Section 7.4.

The second option is to learn how to use an existing emulator, and read the source code (MATLAB applications are collections of M-files, it is quite easy to inspect them and modify the code as we deem fit). There are a few quantum emulators in MATLAB. A very good and quite documented library is Quack, developed by Peter Rohde at the Department of Physics of University of Queensland, Australia. We are going to show a few things that Quack can do, and then it is your game: you can download it, learn the few examples, and start playing right away.3

The first thing one needs to do is to initialize Quack:

image quack

Welcome to Quack! version pi/4 for MATLAB
by Peter Rohde

Centre f or Quantum Computer Technology, Br i sbane, Australia
http://www.physics.uq.edu.au/people/rohde/

 

Now, we initialize a two-qubit register to the ground state(|00):

image init_state(2)

Just for fun, let us change the first qubit

image prepare_one(1)

To see what happens, print the circuit history:

image print_hist

{

[1, 1] = |1 > –

[2, 1] = |0 > −

}

 

Note: Ignore the left side of the circuit (it is there just to keep track of which cells contain the information). The right side of the equation contains the entry points of the circuit (in this case two qubits, initialized to image, and image respectively). As we shall see momentarily, the circuit grows by concatenating gates and measurements.

Let us measure now the first qubit:

image Z_measure(1)

ans=−1

 

and the second:

image Z_measure(2)

ans=1

 

Notice that the answer is −1 for image and 1 for image. This may be a bit confusing at first, but is consistent with the spin notation: image simply means spin down along the z axis.

image print_hist

{

[1, 1] = |1 > – < Z| – – – – –

[2, 1] = |0 > – – – – – < Z| –

}

 

How about applying a controlled-NOT using the first qubit as control?

image cnot(1, 2)

Better check what is happening …

image print_hist

}

[1, 1] = |1 > – < Z| – – – – – o

[2, 1] = |0 > – – – – – < Z| – X

}

 

And now let us apply Hadamard on the second qubit:

image H(2)

print_hist

{

[1, 1] = |1 > – < Z| – – – – – o– – –

[2, 1] = |0 > – – – – – < Z| – XH

}

 

What if we liked to shift the phase of the first qubit? T (see Section 5.4, it is there for you):

image T(1)

image print_hist

{

[1, 1] = |1 > – < Z| – – – – – o– – – T

[2, 1] = |0 > – – – – – < Z| – XH– – –

}

 

Let us perform some measurement. This time we are going to measure the second qubit, again along the z axis (i.e., in the standard basis):

image Z_measure(2)

ans = 1

 

Hint: Pause a moment, jot down the steps, and try to follow what happened.

We have written a simple circuit, provided an input, and measured the results. Now it is all up to you: there are plenty of other gates, initialization routines, and measurement options in Quack, which you can dig up by reading the documentation. Investigate what is already there and play. (By the way, Quack can be easily extended in a number of ways. For instance, you can provide a simple GUI for designing circuits. You may want to start a mini project and create your personalized quantum computing lab).

Have fun!