-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclone.py
64 lines (50 loc) · 1.93 KB
/
clone.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#
# Example of cloning a qubit that is currently in superposition to another qubit.
#
import qiskit
from qiskit import ClassicalRegister, QuantumRegister, QuantumCircuit
from qiskit import IBMQ
from configparser import RawConfigParser
type = 'sim' # Run program on the simulator or real quantum machine.
def run(program, type, shots = 100):
if type == 'real':
global isInit
if not run.isInit:
# Setup the API key for the real quantum computer.
parser = RawConfigParser()
parser.read('config.ini')
IBMQ.enable_account(parser.get('IBM', 'key'))
run.isInit = True
# Set the backend server.
backend = qiskit.providers.ibmq.least_busy(qiskit.IBMQ.backends(simulator=False))
# Execute the program on the quantum machine.
print("Running on", backend.name())
job = qiskit.execute(program, backend)
return job.result().get_counts()
else:
# Execute the program in the simulator.
print("Running on the simulator.")
job = qiskit.execute(program, qiskit.Aer.get_backend('qasm_simulator'), shots=shots)
return job.result().get_counts()
run.isInit = False
# Setup qubits.
qr = QuantumRegister(2)
cr = ClassicalRegister(2)
program = QuantumCircuit(qr, cr);
# Set the first qubit to 1; this is the value we want to clone to the second qubit.
program.x(qr[0]);
# Put the first qubit into superposition.
program.h(qr[0]);
# To clone this qubit to the second, first undo the superposition.
program.h(qr[0]);
# Perform a CNOT between the qubits to copy the value from the first to the second.
program.cx(qr[0], qr[1])
# Put the qubits back into superposition; the values are now cloned.
program.h(qr[0])
program.h(qr[1])
# Measure the value of the qubits to confirm they are equal. We do this by first taking them out of superposition to get the actual value, then measuring the result.
program.h(qr[0])
program.h(qr[1])
program.measure(qr, cr);
# Execute the program.
print(run(program, type))