oreilly-qc / oreilly-qc.github.io

Code samples for Programming Quantum Computers, from O'Reilly Media
135 stars 62 forks source link

Qiskit version of sample 3-3 #17

Open machinelevel opened 4 years ago

machinelevel commented 4 years ago

(sent from a reader to @mercedesGS ) This is regarding QISKIT code sample 3-3: Phase Kickback. According to the book, value of register 2 shall be 1 not 0.

However, if you run QISKIT 3-3 sample code, you can see that it’s 0.

## Example 3-3: Phase Kickback
# Set up the program
reg1 = QuantumRegister(2, name = 'reg1')
reg2 = QuantumRegister(1, name = 'reg2')
qc = QuantumCircuit(reg1, reg2)

qc.h(reg1) # put it into reg1 superposition of 0 and 1
qc.cu1(math.pi/4, reg1[0], reg2)
qc.cu1(math.pi/2, reg1[1], reg2)

backend = BasicAer.get_backend('statevector_simulator')
job = execute(qc, backend)
result = job.result()

outputstate = result.get_statevector(qc, decimals=3)
print(outputstate)
qc.draw()

How can I change it to 1 from 0 for register 2?