ECS189A-Python代写
时间:2023-02-11
ECS 189A是一门面向留学生的电子工程与计算机科学课程。课程将带领学生探索电子工程与计算机科学领域的前沿技术,包括计算机网络、物联网、人工智能等。通过课程的学习,学生将获得对这些技术的深刻理解,并学会如何使用它们解决实际问题。课程采用了实际项目实践和课堂讲解相结合的教学方法,以帮助学生更好地掌握课程知识。总之,ECS 189A是一门充满挑战性和实用价值的课程,适合所有对电子工程与计算机科学感兴趣的留学生。
ECS 189A: Project 1
2/10/2023
Due: 2/17/2022
Congratulations! You have already come a long way. You’ve learned the basics of quantum computation,
on how the unitary operations and measurements work, even when you have many qubits. You have also
learned various quantum gates. In this project, you will get to play with a quantum computing simulator.
A quantum computing simulator is exactly what it sounds like. It’s not a quantum computer; rather, it
is a classical software that mimics the behavior of quantum computer. Such simulation can be costly, often
taking a computational resource that scales exponentially with the number of qubits. However, modern
computers are very good, so for a small number of qubits, they can work reasonably well, even on your own
computer.
The simulator we use will be IBM’s Qiskit package. Please follow the instruction on this website.ß
ßß ßßßßßß ßß ßß ßß ßß ßß ßß ßßß ßßß
ß ß
How to submit your project: Store each of your files as Sol1.py, Sol2.py, . . ., Sol4.py. Please
upload your solutions to Canvas (Not Gradescope!).
1. (3 Points) Write a function that, for any θ ∈ R, applies the single-qubit gate
U =
(
cos θ sin θ
− sin θ cos θ
)
to the state |0⟩ and returns that state.
To submit your answer, please follow the instruction. Your python file should be of the following form.
1 from qiskit import QuantumCircuit
2
3 def rotation(theta):
4 qc = QuantumCircuit(1)
5 # Your answer goes here.
6 # Apply gates using operations such as qc.h(0).
7 return qc
The returned object, qc, will contain the result of the computation. You can use any of the gates in this
website except the general U -gate. (That will make the problem too easy, wouldn’t it?) (Hint: You may
find the P -gate to be useful, which is equivalent to the rz-gate up to a global phase.)
2. (4 Points) One of the basic subroutines that people frequently use in quantum computing is parity check.
In this problem, you will be asked to write a program that measures the parity of some bits. The parity of
1
the bits x1, . . . , xn is defined as
x1 ⊕ . . .⊕ xn.
Assume that you are given a 4-qubit quantum state, stored in qc. Each qubit shall be labeled by 0, 1, 2,
and 3. The state of the qubit 3 will be |1⟩, and the remaining qubits can be in an arbitrary state. Apply a
sequence of quantum gates so that the parity of qubit 0, 1, and 2 gets stored in 3.
To submit your answer, please follow the instruction. Your python file should be of the following form.
1 from qiskit import QuantumCircuit
2
3 def parity(qc):
4 # Here qc will be created externally via qc = QuantumCircuit(4),
5 # followed by qc.x(3)
6 # Apply gates using operations such as qc.h(0), qc.cnot(1,3), etc.
7 return qc
3. (4 Points) Again assume that you are given a 4-qubit quantum state, stored in qc, following the
convention is Problem 2. As before, the state of the qubit 3 will be |1⟩. Use the phase kick-back operation
to apply
|1⟩ ⊗ |x⟩ → (−1)f(x)|1⟩ ⊗ |x⟩,
where f(x) = OR(x0, x1) ⊕ OR(x1, x2) and x = x2x1x0 is a 3-bit number. To be extra clear, because
x = x2x1x0 is a 3-bit number, the equation above is really a short-hand notation of
|1⟩ ⊗ |x2⟩ ⊗ |x1⟩ ⊗ |x0⟩ → (−1)f(x2x1x0)|1⟩ ⊗ |x2⟩ ⊗ |x1⟩ ⊗ |x0⟩,
To submit, use the format in Problem 2. (Hint: You may need to use the Toffoli gate.)
4. (4 Points) Uh-oh! Your friend tried to apply the phase kickback operation for the following function:
f(x) = AND(AND(x0, x1), x2),
but it does not seem to work. Here is the code.
1 from qiskit import QuantumCircuit
2
3 def cccx(qc):
4 # Here qc will be created externally via qc = QuantumCircuit(5).
5
6 # Prepare (|0>−|1>)/ (square root(2)) state on qubit 4.
7 qc.h(4)
8
9 # Compute the function.
10 qc.ccx(0,1,3)
11 qc.cxx(2,3,4)
12
13 # Return the state of qubit 4 back to |0>.
14 qc.h(4)
15 return qc
Debug this code, so that the desired phase kick-back operation works as expected.
Here are some hints.
2
• Note that the state of qubit 3 and 4 should be |0⟩ in the end. You can assume that both qubits start
in |0⟩ as well.
• Recall what the phase kickback does. If the bits x0, x1, x2 are all 1, we should accrue a phase of −1.
For all other cases, we should only get +1.
3
How to test your code: To debug your code, it will be helpful to run your code on a simulator. Here is
a simple way to set this up, which may be helpful in solving Problem 1.
1 # Setting up
2 from qiskit import Aer, execute, QuantumCircuit
3 from qiskit.quantum_info import Statevector
4 backend = Aer.get_backend("statevector_simulator")
5
6 # Writing your code.
7 def rotation(theta):
8 qc = QuantumCircuit(1,1)
9 # Your answer goes here.
10 # Applygates using operations such as qc.h(0).
11 return qc
12
13 # Testing your code.
14 # I will choose a particular theta and see what kind of state we get
back.
15 theta = 0.2
16 test = rotation(theta)
17 result = execute(test, backend=backend, shots=1).result()
18 print(result.get_statevector())
This code will show a two-dimensional array, which stores the amplitude of the state vector. Specifically,
suppose the state vector is α|0⟩+ β|1⟩. The printed format will be [α, β].
For the other problems, you can use the following format.
1 # Setting up
2 from qiskit import Aer, execute, QuantumCircuit
3 from qiskit.quantum_info import Statevector
4 backend = Aer.get_backend("statevector_simulator")
5
6 # Writing your code.
7 def parity(qc):
8 # Here qc will be created externally via qc = QuantumCircuit(4)
9 # followed by qc.x(3)
10 # Apply gates using operations such as qc.h(0), qc.cnot(1,3), etc.
11 return qc
12
13 # Testing your code.
14 # I will set the input bit string to be x_0=1, x_1=1, x_2=1
15 qc = QuantumCircuit(4)
16 qc.x(0)
17 qc.x(1)
18 qc = parity(qc)
19 result = execute(qc, backend=backend, shots=1).result()
20 print(result.get_statevector())
Here qc.x(0) and qc.x(1) are flipping the input qubits from |0⟩ to |1⟩. (Recall that when we initialize
QuantumCircuit, all the qubits are initialized to |0⟩.) This code will show a 16-dimensional array. The
k’th entry of the array represents the state |Bin(k)⟩, where Bin(k) is the binary represetnation of k. For
instance, Bin(8) = 1000, Bin(15) = 1111, Bin(7) = 0111, etc.
essay、essay代写