COMP SCI 4413, 4813, 7413 Introduction to Quantum Computing
Assignment 2
Author: Yasir Latif (S2 2019), Tat-Jun Chin
Submit on MyUni before 11:59pm Monday 2 May 2022
Instructions
Attempt all sections. If necessary, refer to Rieffel and Polak [RP] for more information.
The deliverable is a program for Section 1 to be submitted via MyUni.
1 Quantum circuit simulator
In Python, implement a simple quantum simulator. Write your program in the file quantum.py.
Your program must be able to be compiled and run as follows:
$ python quantum.py [circuit]
Make sure that your Python program is compatible with Python v3.6.9.
The input to your program is a text file specified by the path [circuit]. Take note that
I do not have a Windows machine and it will be difficult for me to locate one; thus
I will be running your program on a Unix-like environment (e.g., Linux, Mac OS X) and the
path [circuit] will follow the Unix convention.
If you do not have a Unix-like machine, consider dual booting your machine with Linux or use a
simulation environment such as Cygwin for Windows.
In this assignment, we will only consider two-qubit systems. The input text file pointed to
by [circuit] contains the design of a quantum circuit and desired input quantum states. The
specific formatting of the file is as follows:
T M
L11 L12 … L1T
L21 L22 … L2T
Q11 Q12 Q13 Q14
Q21 Q22 Q23 Q24
. . . .
. . . .
. . . .
QM1 QM2 QM3 QM4
1
More details:
The first line contains the number T of quantum transformations performed by the circuit (T is
the number of“columns” or steps in the circuit), and the number M of input quantum states to
process.
The subsequent two lines correspond to the two“wires” in the circuit. Each character Lnt (e.g.,
L25 is the character at the 2nd wire and 5th quantum transformation) can be one of the following
symbols:
- Identity (single qubit).
X Not (single qubit).
Z Phase shift (single qubit).
Y Negation and phase shift (single qubit).
H Hadamard (single qubit).
. Control bit. - Target bit.
The subsequent M lines define the input quantum states in linear algebra (vector) form, where
Qm1 Qm2 Qm3 Qm4
are the coefficients of the m-th (1 ≤ m ≤M) input quantum state
Qm1 |00〉+Qm2 |01〉+Qm3 |10〉+Qm4 |11〉 . (1)
Example: For the quantum circuit below,
H X H
Y Z
where we wish to evaluate the following input states,
|v〉 = |11〉
|v〉 = 1√
2
(|00〉+ |10〉
|v〉 = 0.7555 |00〉+ 0.3065 |01〉+ 0.4257 |10〉+ 0.3925 |11〉
the corresponding input text file contains
2 - 3
H.X+H.
-+-.YZ - 0 0 1
0.7071 0 0.7071 0
0.7555 0.3065 0.4257 0.3925
Given the path [circuit] to an input text file in the correct format, your program should
print to standard output (i.e., the command line) the output quantum states obtained by processing
the input states using the given quantum circuit. Specifically, for the input file that contains
T M
L11 L12 … L1T
L21 L22 … L2T
Q11 Q12 Q13 Q14
Q21 Q22 Q23 Q24
. . . .
. . . .
. . . .
QM1 QM2 QM3 QM4
your program should print
S11 S12 S13 S14
S21 S22 S23 S24
. . . .
. . . .
. . . .
SM1 SM2 SM3 sM4
to the standard output, where
Sm1 Sm2 Sm3 Sm4
are the coefficients of the output quantum state
Sm1 |00〉+ Sm2 |01〉+ Sm3 |10〉+ Sm4 |11〉 (2)
corresponding to the m-th input quantum state.
1.1 Allowable external libraries
You are allowed to use standard Python libraries to write your pathfinding program (see https:
//docs.python.org/3/library/ for the components that make up the Python v3.6.9 stan-
dard libraries). In addition to the standard libraries, you are allowed to use NumPy. No other
external libraries are allowed.
3
1.2 Submission
Submit your program files in a zip package named assignment2.zip on MyUni.
1.3 Evaluation
I will be testing your program using several quantum circuits to verify its correctness. If your
program gives the correct output to all tests, you will receive all the marks. - Self-testing
To help you verify the correctness of your program, answer the following questions. You do not
need to submit your answers to the questions.
Question 1 Equivalent gate
What do the following circuits do? Check using your quantum simulator.
H H
H H
H H
Question 2 Inverting control bit
The following circuit applies transformation U on the second qubit if the first qubit is in state 1.
U
Design a circuit that applies transformation U on the second qubit if the first qubit is in state 0.
Verify using your quantum simulator.
Question 3 Generating EPR pairs
4
Define a circuit that can create EPR pairs. This circuit takes as input |00〉 and produces 1√
2
(|00〉+
|11〉). Verify the correctness of your circuit using your simulator.
What happens if the input is not |00〉? Is the output state of your circuit still entangled?
Question 4 Dense coding
Using your simulator, implement a circuit to perform dense coding as described in the lectures.
The circuit takes |00〉 as input, creates an entangled pair (Question 1) and then perform encoding.
The second part of the circuit decodes the incoming bits to extract two classical bits.
How critical is the choice of input to this circuit?