There is no better way to learn than playing. After all, that is how children learn. In this appendix, we are going to provide the basic guidelines for “playing the quantum computing game” with the help of the MATLAB environment.
Reader Tip. This is not a full MATLAB tutorial. We assume that a fully functional version of MATLAB is already installed on your machine and that you know how to start a session, perform some basic calculations, save them, and quit. You should also know what M-files are and how to load them. For a crash brush up, you can read the online tutorial by MathWorks: http://www.mathworks.com/academia/student_center/tutorials/launchpad.html.
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
c = 5 + i
and the computer will respond with
c = 5.000 + 1.000i
Or, equivalently,
c = 5 + j
c = 5.000 + 1.000i
Adding and multiplying complex numbers are entirely straightforward:
d = 3 − 2i
d = 3 − 2i
s = c + d
s = 8 + 3i
p = c * d
p = 25 + 5i
There is also a handy complex conjugate:
c1 = conj(c)
c1 = 5.000 − 1.000i
You may get the real and imaginary parts of a complex number by typing
re = real(c)
re = 5
im = imag(c)
im = 5
One can switch from Cartesian representation to polar representation:
r = abs(c)
r = 7.0711
angle(c)
ans = 0.78540.
And back from polar to Cartesian:
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:
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:
bra = [1, 2 − i, 3i]
bra = 1 + 0i 2 − 1i 0 + 3i
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):
bra*ket
ans = 15
The norm function is built-in:
norm(ket)
ans = 3.8730
So much for vectors. Let us declare a matrix, say, Hadamard:
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):
inv(H)
ans =
0.70711 0.70711
0.70711 −0.70711
MATLAB provides a trace function:
trace(H)
ans = 0
The product is straightforward. (Caveat: If you make a dimensional mismatch, MATLAB will return an error!)
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:
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.
[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.
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:
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):
init_state(2)
Just for fun, let us change the first qubit
prepare_one(1)
To see what happens, print the circuit history:
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 , and respectively). As we shall see momentarily, the circuit grows by concatenating gates and measurements.
Let us measure now the first qubit:
Z_measure(1)
ans=−1
and the second:
Z_measure(2)
ans=1
Notice that the answer is −1 for and 1 for . This may be a bit confusing at first, but is consistent with the spin notation: simply means spin down along the z axis.
print_hist
{
[1, 1] = |1 > – < Z| – – – – –
[2, 1] = |0 > – – – – – < Z| –
}
How about applying a controlled-NOT using the first qubit as control?
cnot(1, 2)
Better check what is happening …
print_hist
}
[1, 1] = |1 > – < Z| – – – – – o–
[2, 1] = |0 > – – – – – < Z| – X–
}
And now let us apply Hadamard on the second qubit:
H(2)
print_hist
{
[1, 1] = |1 > – < Z| – – – – – o– – –
[2, 1] = |0 > – – – – – < Z| – X– H–
}
What if we liked to shift the phase of the first qubit? T (see Section 5.4, it is there for you):
T(1)
print_hist
{
[1, 1] = |1 > – < Z| – – – – – o– – – T –
[2, 1] = |0 > – – – – – < Z| – X– H– – –
}
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):
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!
1 Signal processing engineers tend to use “j” to denote the imaginary unit, hence the notation.
2 A big caveat: In MATLAB matrices and vectors start at 1, not 0!
3 Although Quack is not at present a huge library, it is not a toy either. It contains functionality for quantum computation that goes beyond the scope of this book. But do not get deterred: you can use what you need, and perhaps, learn more as you go along.