How can I use X gate (quantum not) to convert each of the pure states |0011⟩, |1100⟩, |0101⟩ to |1111⟩ with Qiskit?
My programming assignment is
Using the idea of a multi-controlled phase gate, now use X-gates (quantum not) to convert each of the pure states ||0011⟩,||1100⟩,||0101⟩ each to ||1111⟩ and use MCP gate above to mark the phase. Remember to use X-gates to convert the inputs back.
I have found the way to invert to -||1111⟩ part but still not able to find away to convert ||0011⟩,||1100⟩,||0101⟩ to ||1111>
Here are the code.
from qiskit import QuantumRegister, QuantumCircuit, AncillaRegister
from numpy import pi
def mark_pure_states(qc, b0 , b1, b2, b3):
# mark the components corresponding to the pure states |0011> , |1100>, |0101>
# your code here
bits = [b0,b1,b2,b3]
# TODO: convert |0011> , |1100>, |1010> to |1111>
qc.x(b0)
qc.x(b1)
qc.mcx([b0,b1,b2],b3)
qc.barrier()
qc.x(b1)
qc.x(b3)
qc.mcx([b0,b1,b2],b3)
qc.barrier()
qc.x(b2)
qc.x(b3)
qc.mcx([b0,b1,b2],b3)
qc.barrier()
qc.mcx([b0,b1,b2],b3)
# Use Multi-controlled phase gate to convert |1111> to -|1111>
qc.mcp(pi,[b0,b1,b2], b3)
# Use X gate from -|1111> back to |1111>
for b in bits:
qc.x(b)
b = QuantumRegister(4, 'b')
qc = QuantumCircuit(b)
# create uniform super position
qc.h(b[0])
qc.h(b[1])
qc.h(b[2])
qc.h(b[3])
qc.barrier()
mark_pure_states(qc, b[0], b[1], b[2], b[3])
display(qc.draw('mpl', style='iqp'))
# use a state vector simulator to obain the marked states
backend = Aer.get_backend('statevector_simulator')
job = execute(qc, backend)
result = job.result()
state_vector = result.get_statevector()
print(state_vector)
for i in range(16):
if i == 3 or i == 10 or i == 12: # are we marking the correct basis states
assert abs(state_vector[i] +0.25) <= 0.001
else:
assert abs(state_vector[i]-0.25) <= 0.001
My output right now is:
Statevector([-0.25+3.061617e-17j, 0.25+0.000000e+00j, 0.25+0.000000e+00j,
0.25+0.000000e+00j, 0.25+0.000000e+00j, 0.25+0.000000e+00j,
0.25+0.000000e+00j, 0.25+0.000000e+00j, 0.25+0.000000e+00j,
0.25+0.000000e+00j, 0.25+0.000000e+00j, 0.25+0.000000e+00j,
0.25+0.000000e+00j, 0.25+0.000000e+00j, 0.25+0.000000e+00j,
0.25+0.000000e+00j],
dims=(2, 2, 2, 2))
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
Cell In[241], line 22
20 assert abs(state_vector[i] +0.25) <= 0.001
21 else:
---> 22 assert abs(state_vector[i]-0.25) <= 0.001
AssertionError:
My result should have 3rd, 10th, 12th value of state vector arrays being minus 0.25