CSE1010 Final Group Project
In this project, you will write a basic Quantum Computation and Communication simulator.
Instructions: First read this document through. Then, look at the ZyBooks lab and you will see a
skeleton code already setup. At certain points there are comments indicating which function is for
which task. Furthermore, there are comments that say ##YOUR CODE HERE. These are hints that
you need to write something there. It is highly recommended you do not remove any code from the
skeleton file.
There are five tasks in this project and you will need to implement a function or functions for each of
them. The skeleton code already has a lot setup for you to get you started. Please refer to the Chapter
15 Zybooks lab for the programming portion of the project.
In addition, your group must make a small presentation during your lab session using Blackboard
Collaborate. During this presentation you should discuss what you learned, talk about your solution,
and any comments you have on the “open ended” “why?” questions listed in Task 5.
About Quantum Computing:
We are all used to classical bits: 0's and 1's, which make up our current computer and communication
systems. Quantum bits (also called qubits) are very different:
1. Qubits can be in a 0 or 1 state (which we denote |0> or |1>) like a classical bit, however, unlike
a classical bit, they can also be in a superposition state – a combination of |0> and |1>. We
denote this by:
x|0> + y|1>
where “x” and “y” are real or complex numbers (for this project you may assume they are real
numbers) with the following property:
x2 + y2 = 1.
The “x” and “y” are called probability amplitudes.
2. Unlike classical bits which you can read any time, quantum bits are measured. This process of
measuring a qubit is destructive and potentially random.
3. You cannot copy an unknown qubit (unlike a classical bit which you can copy as many times as
you like)
To model a qubit mathematically (and using a classical computer), we use vectors. The state |0> is the
vector (1,0) while the state |1> is the vector (0,1). Thus, the superposition state:
x|0> + y|1>
is the vector:
x(1,0) + y(0,1) = (x,0) + (0,y) = (x,y).
>> Therefore, in your simulator, you may represent any qubit as a Python list: [x,y]
Task 1: Write a function createQubit. It takes two inputs: alpha and beta. These are floating point
values. It should return a list [x,y] where:
x = alpha / N
y = beta / N
and:
N = sqrt(alpha2 + beta2)
This makes sure that the list [x,y] is a proper qubit (in that it is normalized: x2 + y2 = 1). (Check this
yourself on paper and make sure x2 + y2 = 1)
Now, measuring a qubit is a destructive process. There are also infinitely many ways to perform a
measurement. We will simulate only two ways: Z basis measurements and X basis measurements.
If you're curious as to why the are called Z and X basis measurements, they have to do with the
eigenvectors of the Pauli Z and X operators. However, knowledge of this is not needed to perform the
tasks here!
Let's consider a quantum state [x,y] which, recall, means the superposition state:
x|0> + y|1>
Now, to perform a Z basis measurement is “easy”: first, you “plug in” your quantum state. Next the
measurement device will “click” (i.e., read or output) either “0” or “1” (but not both). The probability
that it clicks “0” is simply x2. The probability that it clicks “1” is y2. (Recall that x2 + y2 = 1 so this
makes sense!).
For example, if the input state is:
|+>= 1
√2
|0>+ 1
√2
|1> (1)
Then, after measurement, the device will read /click “0” with probability 1/2 or it will read “1” with
probability 1/2. Furthermore, the quantum state undergoes state collapse. That means that, no matter
what the input state originally was, after measurement the state becomes what you observe. So, if the
state was originally |+> (defined above), then if after measuring, the measurement device clicks “0”,
the qubit now really is |0>. The original state, |+>, was destroyed!
Task 2: Write a Z basis measurement function. It should take as input a state [x,y] and simulate a Z
basis measurement. Namely, given [x,y], your simulator should compute: what is the probability of
observing |0>; what is the probability of observing |1>. Then, based on this probability, choose a
random outcome.
An X basis measurement is a little more involved but not much. For this measurement, given [x,y], the
probability of observing “0” is now:
0.5 * (x2 + y2) + xy
The probability of observing “1” is now:
0.5*(x2 + y2) – xy
Note that, now, the probability of observing “0”, given the input state |+> defined above, is always 1!
Task 3: Write an X basis measurement function. Similar to the Z basis one, first compute the
probability of observing 0 or 1. Then simulate the measurement by choosing a random outcome
according to the correct distribution.
Quantum Tomography
Let's say I give you a quantum state |q> but you don't know what it is (i.e., you don't know what the “x”
and the “y” are). If you measure in the Z basis, you will observe one outcome or the other (Be careful:
the simulator may know the probability distribution, but in real life you do not know that from a single
arbitrary state!). If you measure and the device clicks “0” what does this tell you? Not too much. But
now, let's say I give you 1000 copies of |q>. You measure in the Z basis and observe 250 “0”s and 750
1's. This may lead you to conclude that:
|q>=√14 |0>+√34 |1> (2)
Since 250/1000 = ¼ and 750/1000 = ¾. Note the square roots because probability amplitudes are
always the square root of the probability!
This is an example of a (simplified) version of quantum tomography. The idea of taking multiple
copies of an unknown quantum state, subjecting them to measurements, and learning what the
underlying state actually is. (Comment: In real life you need to perform measurements in other bases
to really know the state).
Task 4: Write a simplified quantum tomography simulator. It should be a function that takes as input a
quantum state [x,y] and an integer value “numTrials.” Then, it should repeat the following (simulated)
experiment “numTrials” times:
1. Take a copy of the state and measure it in the Z basis to get an outcome 0 or 1
2. Keep count of how many times you observe 0 or 1
After repeating the above, use this information to guess at “x” and “y”. The guess for x should be
based on the number of times you observe 0; for y it should be based on the number of times you
observe 1. Indeed, from Task 2, note that p0 is x2. You are trying to estimate p0 and, in so doing,
estimating x (similarly y). Be careful about not forgetting square roots in your answer....
Output the following data:
Estimated x is ...
Estimated y is ...
where the … are your guesses. Next, output:
The error is E
where E is the computed squared error, namely:
E = (guess_X – x)2 + (guess_y – y)2,
where guess_x and guess_y are your guesses from the tomography experiment.
Finally, test your function with numTrials at 10, 100, 1000, and 10000 to see how the error diminishes.
Use various x and y.
Quantum Key Distribution
One very useful application of quantum communication is through quantum key distribution (QKD).
The goal of such a protocol is to allow two parties Alice (A) and Bob (B) to establish a shared secret
key – that is, a string of classical bits which are correlated and secret. Key distribution using only
classical communication is impossible unless you make assumptions on the adversary's computational
power (the adversary is attempting to learn information on the secret key that Alice is trying to send to
Bob). However, using quantum communication, it is possible without making any assumptions.
Here you will simulate a basic QKD protocol called the BB84 protocol. The goal is for Alice to
transmit a key to Bob. So the protocol repeats the following process:
1. First, Alice chooses a random key bit (50/50 it's 0 or 1).
2. Next, Alice chooses a random basis Z or X (50/50)
3. Alice now prepares a qubit using the following rules:
1. If the key is 0 and the basis is Z, she sends |0> = [1,0]
2. If the key is 1 and the basis is Z, she sends |1> = [0,1]
3. If the key is 0 and the basis is X, she sends |+> = [1/sqrt(2), 1/sqrt(2)]
4. If the key is 1 and the basis is X, she sends |-> = [1/sqrt(2), -1/sqrt(2)]
The qubit Alice prepares is sent to Bob.
4. When Bob gets the qubit, he chooses a random basis Z or X (50/50) independently of Alice.
Alice BobAdversary
|0>
Key=0
basis=Z
Then, he measures the qubit in the chosen basis to get a key bit (0 or 1).
5. Finally, Alice tells Bob what basis she used.
1. If they match (Alice and Bob used the same basis this time), they add their key bits to their
shared key. They then repeat
2. If the do not match, they throw out this round and repeat
Task 5: Write a function that simulates the above BB84 protocol “numTrials” time. In the skeleton
code, AlicePrepare() is already written for you. Take a look at it to see how it works. You will need to
write functions for Bob and also a function to simulate the entire protocol calling these functions.
Your function should do that following:
1. Simulate the BB84 protocol and every time Alice and Bob use the same basis, they append their key
bits (which can be integers 0 or 1) to a list “aliceKey” and a list “bobKey” variable.
2. After “numTrials”, print the following:
Size of key is …
where … is the length of the final keyA list (the length of keyB should be identical – why?) Finally,
your function should return “aliceKey” (which should match “bobKey” - why?)