Homework 3
Due on October 9, 2019 at midnight.
NOTE: The due date is one day after the midterm exam. Obviously, it would be a good idea to do this homework before the exam,
because the content will be covered on the exam. But we also want to give you the flexibility to use your time to your best advantage.
Important: Write your name and Unity ID in every submission file. Name the submission files correctly as asked for.
- (25 points) Toffoli gate implementation
The picture below shows one implementation of a Toffoli gate using 1-qubit gates and CNOT.
(Source: Wikipedia)
Implement this Toffoli circuit in Qiskit, as follows:
- Define a function toffoli that takes a circuit and three qubits (which are already in the circuit)
and adds the gates as needed. The T gate is t(qubit) and T-dagger is tdg(qubit).
- Create quantum registers. You will need two control qubits and one target qubit. (This can be one or
multiple registers -- up to you.) Create a classical register that will be used to measure the three qubits.
- Create four separate circuits to demonstrate the correct operation of the Toffoli implementation.
Each circuit will have a different initialization of the control qubits (00, 01, 10, 11). Print the results
of executing each circuit using the qasm_simulator backend. Use 100 shots in each simulation, to
"prove" that you get correct result 100% of the time. NOTE: You can use the same the same set of registers
for all the circuits, and you can use a single backend object for all the simulations.
- At the end of your Python script, draw the circuit with input 00.
You can implement using either Python (.py) or a Jupyter notebook (.ipynb). Submit your solution either as
toffoli.py or as toffoli.ipynb.
- (20 points) 1-bit full adder
The files half_adder.py and half_adder.ipynb show an
implementation of the gate-model half-adder circuit discussed in class. Download your preferred version of
the file and use it as a starting point to implement a full adder circuit. (We're providing this starter file
so that you'll follow the directions below!)
- The full adder function takes an additional input: the carry-in bit from a previous stage.
- When creating registers, use name strings to make the circuit easier to read.
- Prepare the input qubits to be a superposition of all possible inputs. Use a barrier before and after
the function call to clearly separate input prep, computation, and measurement.
- Measure the output qubits only, with the carry-out as the most significant and the sum as the least
significant.
- Simulate the circuit for 4096 shots and print the results.
- At the end, draw the circuit and plot a histogram of the results.
Submit your solution as full_adder.py or full_adder.ipynb.
- (30 points) n-bit adder
Using the previous exercise as a starting point, implement a generalized n-bit adder in Qiskit.
- The adder function takes an integer n, two n-qubit input registers, and an n+1-qubit output register.
Note that there is no carry input. An n-bit adder is composed from one half-adder and n-1 full adders.
- You will need to create new qubits inside the adder function, to communicate the carry bits from one
stage to the next. Use add_register to add these to the circuit. For this problem, don't worry
about uncomputing these ancilla qubits.
- As in the previous exercise, only measure the output qubits. This is intepreted as an n+1-qubit sum.
- Use the adder function to create a 4-bit adder circuit (two 4-bit inputs and a 4-bit output).
- With an equal superposition of input states, simulate the 4-bit adder circuit for 4096 shots and print the results.
- At the end, draw the circuit and plot a histogram of the results.
- Convince yourself that your implementation works for values of n from 2 to 6, but only submit a
four-bit circuit.
Submit your solution as adder.py or adder.ipynb.
- (25 points) Selective phase change
Use Qiskit to create a circuit that performs a selective phase shift on a 3-qubit value. Perform a phase
shift of π (180 degrees) for states where the AND of all three qubits equals 1.
- Perform the selective phase shift on a three-qubit equal superposition state.
- When implementing the 3-bit AND function, if you need ancilla bits, be sure to uncompute them.
- Don't meaure anything. Simulate the circuit using the statevector_simulator. (Only one shot needed.)
Print the results as a state vector.
- At the end of your script, draw the circuit.
- It might be easier to observe the phase difference if you use .round(2) on the state vector
to round the results to two decimal places. You may also want to write some Python code to make the
vector easier to read. For example:
def printvec(vec) :
for i in range(0,vec.size) :
print("{0:05b}: {1}".format(i,vec[i]))
Submit your solution as and3_phase.py or and3_phase.ipynb.