Coding Assignment 2
CS 401: Quantum Computing
CourseNana.COM
Instructions CourseNana.COM
To complete the assignment, please fill in all coding cells as instructed below, and then submit the file on Notebowl once completed. Before you turn in your submission, make sure everything runs as expected. First, restart the kernel (in the menubar, select Kernel → Restart) and then run all cells (in the menubar, select Cell → Run All). Make sure you fill in any place that says YOUR CODE GOES HERE. CourseNana.COM
Constraints on references and collaborations CourseNana.COM
You may not share code or work with others when writing your code.
CourseNana.COMReference the Qiskit Quantum Teleportion guide (This is Chapter 3.11 in the Qiskit online textbook.) CourseNana.COM
Finally: Be sure to use proper coding style for all your solutions! For example, you should write docstrings for all functions, include useful in-line comments explaining how your code works, write clean code with readable variable names, etc.. You will lose points if you write sloppy code with poor style (e.g., not writing docstrings for any of your functions). CourseNana.COM
Best of luck! CourseNana.COM
# Specify your name below
NAME = "Your Name"
# Qiskit and NumPy imports
import numpy as np
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister, assemble, Aer
from qiskit.quantum_info import Statevector
from qiskit.visualization import array_to_latex
from qiskit.visualization import state_visualization
from qiskit.visualization import plot_state_city, plot_bloch_multivector
import qis401, random, math, cmath
Part 1 [20 points]
In this first part, you will implement the Quantum Teleportation algorithm using Qiskit. You must structure your solution by implementing the following API: CourseNana.COM
random_qubit()
: returns a two-element list containing the amplitudes of a randomly selected qubit (i.e., the first value is the amplitude for|0⟩ and the second value is the amplitude for|1⟩ ). To produce the random qubit, you must generate the parameters? and? in the Bloch sphere equation (Equation 1.4 on page 15 in NC) uniformly at random and then use these parameters to calculate each amplitude.
CourseNana.COMinitialize_circuit(teleport_amps)
: Takes in a two-element listteleport_amps
and returns the six tuple(circ, teleport, alice, bob, t_measure, a_measure)
, whereteleport, alice,
, andbob
are theQuantumRegister
objects that will be used in the algorithm;t_result
anda_result
are theClassicalRegister
objects that will be use to store Alice's measurement of theteleport
andalice
registers; andcirc
is the overallQuantumCircuit
object itself. Furthermore, the state ofteleport
should be initialized to be the values inteleport_amps
(which will be created by previously callingrandom_qubit()
). Use theQuantumCircuit
methodinitialize()
to perform this initialization of the teleportation qubit. Also at the end of the function, call thebarrier()
method to block off this initial phase of the algorithm.
CourseNana.COMmake_epr(circ, alice, bob)
: takes in the QT circuit and quantum registers for Alice and Bob and puts them in an EPR pair. At the end of the function, again use thebarrier()
method to block off this segment of the algorithm.
CourseNana.COMalice_computation(circ, teleport, alice, t_measure, a_measure)
: Takes in the QT circuit along with the registers needed to perform Alice's part of the algorithm. Use thebarrier()
method twice to create two blocks, one for her quantum operations and one for her measurements.
CourseNana.COMbob_computation(circ, bob, t_measure, a_measure)
: Takes in the QT circuit along with the registers needed to perform Bob's part of the algorithm CourseNana.COM
Implement the above functions in a single cell. To generate your output, create three additional cells that do the following: CourseNana.COM
- Your first cell should simply create
teleport_amps
by callingrandom_qubit()
. You should then passteleport_amps
into the Qiskit functionplot_bloch_multivector()
to visualize the qubit as a Bloch sphere. The output of the cell should look something like:
Your second cell should then construct the circuit by calling the functions from the API, and then use the
draw()
method to display the circuit. The output of the cell should look similar to the final circuit in the Quantum Teleportation slides from class (along with the initialization block at the beginning that generates the random qubit).
CourseNana.COMYour third cell should then simulate the circuit by calling
qis401.simulate_circuit()
. You should then use the returnedresult
object to get the final state vector, and then pass this intoplot_bloch_multivector()
to visualize the final state ofteleport
,alice
, andbob
. The output should look something like: CourseNana.COM
In particular, the first and second spheres (corresponding to the teleport and Alice qubits) should be pointing either straight up or down (since they've been measured). The third sphere corresponding to Bob's qubit should be pointing in the same direction as the original sphere for the teleportation qubit. CourseNana.COM
Part 2 [30 points]
In this part, you will now implement the Quantum Teleportation algorithm using NumPy vectors and matrices. You will implement the same API as in Part 1. The differences between the functions parameters and return values are specified as follows: CourseNana.COM
random_qubit()
: Reuse the implementation from Part 1.
CourseNana.COMinitialize_circuit(teleport_amps)
: Again takes in a two element listteleport_amps
, but now returns an 8-dimensional NumPy vector that has initialized the amplitudes of the teleportation qubit according toteleport_amps
. Note that in order to store complex values in the NumPy array state vector, you need to initialize the state vector usingzeros(8, dtype=numpy.complex_)
.
CourseNana.COMmake_epr(state)
: Takes in the current state vector and returns the resulting vector after applying the unitary matrices that put Alice and Bob's qubits in an EPR pair. (Like we saw in class, you should use thenumpy.kron()
function to create the unitary matrices corresponding to each gate).
CourseNana.COMalice_computation(state)
: Takes the current state vector and returns a two-tuple(output_state, measure)
, whereoutput_state
is the resulting state after first applying the unitary matrices corresponding to Alice's quantum computations and then measuring the teleport and Alice qubits;measure
should then be one of four strings"00"
,"01"
,"10"
, or"11"
that indicates Alice's measurement result. Note you will have to manually simulate the process of measurement, i.e., you will need to randomly select the measurement outcome and then renormalize the quantum state according to the outcome.
CourseNana.COMbob_computation(state, measure)
: Takes the current state vector and Alice's measurement result, and returns the resulting state vector after applying the unitary matrices corresponding to Bob's computations. CourseNana.COM
Again, implement the functions in the above API in a single cell. (Note to avoid having to repeatedly construct the single qubit matrices for the H, Z, X, and CX gates in each function, it's fine to declare these matrices as global variables at the top). CourseNana.COM
To generate your output, again write a single cell that creates the random teleportation qubit and displays its state as a Bloch sphere. Then, create a second cell that uses the functions in your API to apply the QT algorithm to the intial state vector. At the end of the cell, you should display the state of Bob's qubit as a single Bloch sphere, which should match the original Bloch sphere of the teleportation qubit. CourseNana.COM