mirror of https://github.com/Qiskit/qiskit.git
Update examples for v0.5 (#672)
* remove quantum program from examples * update changelog
This commit is contained in:
parent
c7121fc65e
commit
909728c61d
|
@ -38,6 +38,7 @@ Removed
|
|||
Fixed
|
||||
-----
|
||||
- Fixed ``probabilities_ket`` computation in C++ simulator. (#580)
|
||||
- Fixed the examples to be compatible with version 0.5+ (#672)
|
||||
|
||||
|
||||
`0.5.6`_ - 2018-07-06
|
||||
|
|
|
@ -11,31 +11,17 @@ Note: if you have only cloned the Qiskit repository but not
|
|||
used `pip install`, the examples only work from the root directory.
|
||||
"""
|
||||
|
||||
from qiskit import QuantumProgram
|
||||
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
|
||||
from qiskit import CompositeGate
|
||||
from qiskit.extensions.standard.cx import CnotGate
|
||||
from qiskit.extensions.standard.rx import RXGate
|
||||
|
||||
|
||||
Q_SPECS = {
|
||||
"name": "Program-tutorial",
|
||||
"circuits": [{
|
||||
"name": "Circuit",
|
||||
"quantum_registers": [{
|
||||
"name": "qr",
|
||||
"size": 4
|
||||
}],
|
||||
"classical_registers": [{
|
||||
"name": "cr",
|
||||
"size": 4
|
||||
}]}],
|
||||
}
|
||||
Q_program = QuantumProgram(specs=Q_SPECS)
|
||||
circuit = Q_program.get_circuit("Circuit")
|
||||
quantum_r = Q_program.get_quantum_register("qr")
|
||||
classical_r = Q_program.get_classical_register('cr')
|
||||
quantum_r = QuantumRegister(4, "qr")
|
||||
classical_r = ClassicalRegister(4, "cr")
|
||||
circuit = QuantumCircuit(quantum_r, classical_r)
|
||||
|
||||
circuit.h(quantum_r[0])
|
||||
|
||||
circuit.rx(0, quantum_r[0])
|
||||
|
||||
circuit.cx(quantum_r[0], quantum_r[1])
|
||||
|
@ -46,7 +32,6 @@ circuit.h(quantum_r[0])
|
|||
circuit.cx(quantum_r[0], quantum_r[1])
|
||||
composite_gate_1 = CompositeGate("composite1", [],
|
||||
[quantum_r[x] for x in range(4)])
|
||||
|
||||
composite_gate_1._attach(CnotGate(quantum_r[0], quantum_r[1]))
|
||||
circuit._attach(composite_gate_1)
|
||||
|
||||
|
@ -79,7 +64,5 @@ print("Removed Zero Rotations: " + str(circuit.remove_zero_rotations()))
|
|||
|
||||
print("Removed Double CNOTs: " + str(circuit.remove_double_cnots_once()))
|
||||
|
||||
# QASM from a program
|
||||
|
||||
QASM_source = Q_program.get_qasm("Circuit")
|
||||
QASM_source = circuit.qasm()
|
||||
print(QASM_source)
|
||||
|
|
|
@ -12,39 +12,27 @@ Note: if you have only cloned the Qiskit repository but not
|
|||
used `pip install`, the examples only work from the root directory.
|
||||
"""
|
||||
|
||||
from qiskit import QuantumProgram
|
||||
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
|
||||
from qiskit import register, execute
|
||||
import Qconfig
|
||||
|
||||
###############################################################
|
||||
# Set the backend name and coupling map.
|
||||
###############################################################
|
||||
backend = "ibmqx2"
|
||||
coupling_map = {0: [1, 2],
|
||||
1: [2],
|
||||
2: [],
|
||||
3: [2, 4],
|
||||
4: [2]}
|
||||
backend = "ibmq_5_tenerife"
|
||||
coupling_map = [[0, 1],
|
||||
[0, 2],
|
||||
[1, 2],
|
||||
[3, 2],
|
||||
[3, 4],
|
||||
[4, 2]]
|
||||
|
||||
###############################################################
|
||||
# Make a quantum program for the GHZ state.
|
||||
# Make a quantum circuit for the GHZ state.
|
||||
###############################################################
|
||||
QPS_SPECS = {
|
||||
"circuits": [{
|
||||
"name": "ghz",
|
||||
"quantum_registers": [{
|
||||
"name": "q",
|
||||
"size": 5
|
||||
}],
|
||||
"classical_registers": [
|
||||
{"name": "c",
|
||||
"size": 5}
|
||||
]}]
|
||||
}
|
||||
|
||||
qp = QuantumProgram(specs=QPS_SPECS)
|
||||
qc = qp.get_circuit("ghz")
|
||||
q = qp.get_quantum_register("q")
|
||||
c = qp.get_classical_register("c")
|
||||
q = QuantumRegister(5, "q")
|
||||
c = ClassicalRegister(5, "c")
|
||||
qc = QuantumCircuit(q, c, name='ghz')
|
||||
|
||||
# Create a GHZ state
|
||||
qc.h(q[0])
|
||||
|
@ -59,32 +47,32 @@ for i in range(5):
|
|||
###############################################################
|
||||
# Set up the API and execute the program.
|
||||
###############################################################
|
||||
qp.set_api(Qconfig.APItoken, Qconfig.config["url"])
|
||||
register(Qconfig.APItoken, Qconfig.config["url"])
|
||||
|
||||
# First version: no mapping
|
||||
print("no mapping, simulator")
|
||||
result = qp.execute(["ghz"], backend='ibmq_qasm_simulator',
|
||||
coupling_map=None, shots=1024)
|
||||
print("no mapping, execute on simulator")
|
||||
job = execute(qc, backend='ibmq_qasm_simulator', coupling_map=None, shots=1024)
|
||||
result = job.result()
|
||||
print(result)
|
||||
print(result.get_counts("ghz"))
|
||||
|
||||
# Second version: map to qx2 coupling graph and simulate
|
||||
print("map to %s, simulator" % backend)
|
||||
result = qp.execute(["ghz"], backend='ibmq_qasm_simulator',
|
||||
coupling_map=coupling_map, shots=1024)
|
||||
# Second version: map to ibmq_5_tenerife coupling graph and simulate online
|
||||
print("map to %s, execute on online simulator" % backend)
|
||||
job = execute(qc, backend='ibmq_qasm_simulator', coupling_map=coupling_map, shots=1024)
|
||||
result = job.result()
|
||||
print(result)
|
||||
print(result.get_counts("ghz"))
|
||||
|
||||
# Third version: map to qx2 coupling graph and simulate locally
|
||||
print("map to %s, local qasm simulator" % backend)
|
||||
result = qp.execute(["ghz"], backend='local_qasm_simulator',
|
||||
coupling_map=coupling_map, shots=1024)
|
||||
# Third version: map to ibmq_5_tenerife coupling graph and simulate locally
|
||||
print("map to %s, execute on local simulator" % backend)
|
||||
job = execute(qc, backend='local_qasm_simulator', coupling_map=coupling_map, shots=1024)
|
||||
result = job.result()
|
||||
print(result)
|
||||
print(result.get_counts("ghz"))
|
||||
|
||||
# Fourth version: map to qx2 coupling graph and run on qx2
|
||||
print("map to %s, backend" % backend)
|
||||
result = qp.execute(["ghz"], backend=backend,
|
||||
coupling_map=coupling_map, shots=1024, timeout=120)
|
||||
# Fourth version: map to ibmq_5_tenerife coupling graph and run on ibmq_5_tenerife
|
||||
print("map to %s, execute on device" % backend)
|
||||
job = execute(qc, backend=backend, shots=1024)
|
||||
result = job.result()
|
||||
print(result)
|
||||
print(result.get_counts("ghz"))
|
||||
|
|
|
@ -13,31 +13,17 @@ used `pip install`, the examples only work from the root directory.
|
|||
"""
|
||||
|
||||
import math
|
||||
from qiskit import QuantumProgram
|
||||
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, execute
|
||||
from qiskit.tools.visualization import plot_circuit
|
||||
import Qconfig
|
||||
|
||||
|
||||
###############################################################
|
||||
# Make a quantum program for state initialization.
|
||||
# Make a quantum circuit for state initialization.
|
||||
###############################################################
|
||||
Q_SPECS = {
|
||||
"name": "Program-tutorial",
|
||||
"circuits": [{
|
||||
"name": "initializer_circ",
|
||||
"quantum_registers": [{
|
||||
"name": "qr",
|
||||
"size": 4
|
||||
}],
|
||||
"classical_registers": [{
|
||||
"name": "cr",
|
||||
"size": 4
|
||||
}]}],
|
||||
}
|
||||
Q_program = QuantumProgram(specs=Q_SPECS)
|
||||
circuit = Q_program.get_circuit("initializer_circ")
|
||||
qr = Q_program.get_quantum_register("qr")
|
||||
cr = Q_program.get_classical_register('cr')
|
||||
qr = QuantumRegister(4, "qr")
|
||||
cr = ClassicalRegister(4, 'cr')
|
||||
circuit = QuantumCircuit(qr, cr, name="initializer_circ")
|
||||
|
||||
desired_vector = [
|
||||
1 / math.sqrt(4) * complex(0, 1),
|
||||
|
@ -64,36 +50,22 @@ circuit.measure(qr[1], cr[1])
|
|||
circuit.measure(qr[2], cr[2])
|
||||
circuit.measure(qr[3], cr[3])
|
||||
|
||||
QASM_source = Q_program.get_qasm("initializer_circ")
|
||||
QASM_source = circuit.qasm()
|
||||
|
||||
print(QASM_source)
|
||||
plot_circuit(circuit)
|
||||
|
||||
###############################################################
|
||||
# Set the backend name and coupling map.
|
||||
# Execute on a simulator backend.
|
||||
###############################################################
|
||||
device = 'ibmqx2'
|
||||
coupling_map = {0: [1, 2],
|
||||
1: [2],
|
||||
2: [],
|
||||
3: [2, 4],
|
||||
4: [2]}
|
||||
circuits = ['initializer_circ']
|
||||
shots = 1024
|
||||
|
||||
###############################################################
|
||||
# Set up the API and execute the program.
|
||||
###############################################################
|
||||
Q_program.set_api(Qconfig.APItoken, Qconfig.config["url"])
|
||||
|
||||
# Desired vector
|
||||
print("Desired probabilities...")
|
||||
print(str(list(map(lambda x: format(abs(x * x), '.3f'), desired_vector))))
|
||||
|
||||
# Initialize on local simulator
|
||||
result = Q_program.execute(circuits,
|
||||
backend='local_qasm_simulator',
|
||||
wait=2, timeout=240, shots=shots)
|
||||
result = execute(circuit, backend='local_qasm_simulator', shots=shots).result()
|
||||
|
||||
print("Probabilities from simulator...[%s]" % result)
|
||||
n_qubits_qureg = qr.size
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
"""
|
||||
Example on how to use: load_qasm_file
|
||||
If you want to use your local cloned repository intead of the one installed via pypi,
|
||||
you have to run like this:
|
||||
examples/python$ PYTHONPATH=$PYTHONPATH:../.. python load_qasm.py
|
||||
|
||||
Note: if you have only cloned the Qiskit repository but not
|
||||
used `pip install`, the examples only work from the root directory.
|
||||
"""
|
||||
from qiskit.wrapper import load_qasm_file
|
||||
from qiskit import QISKitError, available_backends, execute
|
||||
try:
|
||||
qc = load_qasm_file("../qasm/entangled_registers.qasm")
|
||||
qc = load_qasm_file("examples/qasm/entangled_registers.qasm")
|
||||
|
||||
# See a list of available local simulators
|
||||
print("Local backends: ", available_backends({'local': True}))
|
||||
|
|
|
@ -13,62 +13,18 @@ used `pip install`, the examples only work from the root directory.
|
|||
"""
|
||||
|
||||
import math
|
||||
from qiskit import QuantumProgram
|
||||
|
||||
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
|
||||
from qiskit import register, execute
|
||||
import Qconfig
|
||||
|
||||
###############################################################
|
||||
# Set the backend name and coupling map.
|
||||
###############################################################
|
||||
backend = "ibmqx2"
|
||||
coupling_map = {0: [1, 2],
|
||||
1: [2],
|
||||
2: [],
|
||||
3: [2, 4],
|
||||
4: [2]}
|
||||
coupling_map = [[0, 1], [0, 2], [1, 2], [3, 2], [3, 4], [4, 2]]
|
||||
|
||||
###############################################################
|
||||
# Make a quantum program for the GHZ state.
|
||||
###############################################################
|
||||
QPS_SPECS = {
|
||||
"circuits": [
|
||||
{
|
||||
"name": "qft3",
|
||||
"quantum_registers": [{
|
||||
"name": "q",
|
||||
"size": 5
|
||||
}],
|
||||
"classical_registers": [{
|
||||
"name": "c",
|
||||
"size": 5
|
||||
}]
|
||||
},
|
||||
{
|
||||
"name": "qft4",
|
||||
"quantum_registers": [{
|
||||
"name": "q",
|
||||
"size": 5
|
||||
}],
|
||||
"classical_registers": [{
|
||||
"name": "c",
|
||||
"size": 5
|
||||
}]
|
||||
},
|
||||
{
|
||||
"name": "qft5",
|
||||
"quantum_registers": [{
|
||||
"name": "q",
|
||||
"size": 5
|
||||
}],
|
||||
"classical_registers": [
|
||||
{"name": "c",
|
||||
"size": 5}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
def input_state(circ, q, n):
|
||||
"""n-qubit input state for QFT that produces output 1."""
|
||||
for j in range(n):
|
||||
|
@ -84,13 +40,11 @@ def qft(circ, q, n):
|
|||
circ.h(q[j])
|
||||
|
||||
|
||||
qp = QuantumProgram(specs=QPS_SPECS)
|
||||
q = qp.get_quantum_register("q")
|
||||
c = qp.get_classical_register("c")
|
||||
|
||||
qft3 = qp.get_circuit("qft3")
|
||||
qft4 = qp.get_circuit("qft4")
|
||||
qft5 = qp.get_circuit("qft5")
|
||||
q = QuantumRegister(5, "q")
|
||||
c = ClassicalRegister(5, "c")
|
||||
qft3 = QuantumCircuit(q, c, name="qft3")
|
||||
qft4 = QuantumCircuit(q, c, name="qft4")
|
||||
qft5 = QuantumCircuit(q, c, name="qft5")
|
||||
|
||||
input_state(qft3, q, 3)
|
||||
qft3.barrier()
|
||||
|
@ -117,25 +71,21 @@ print(qft3.qasm())
|
|||
print(qft4.qasm())
|
||||
print(qft5.qasm())
|
||||
|
||||
|
||||
###############################################################
|
||||
# Set up the API and execute the program.
|
||||
###############################################################
|
||||
qp.set_api(Qconfig.APItoken, Qconfig.config["url"])
|
||||
register(Qconfig.APItoken, Qconfig.config["url"])
|
||||
|
||||
result = qp.execute(["qft3", "qft4", "qft5"], backend='ibmq_qasm_simulator',
|
||||
coupling_map=coupling_map, shots=1024)
|
||||
result = execute([qft3, qft4, qft5], backend='ibmq_qasm_simulator',
|
||||
coupling_map=coupling_map, shots=1024).result()
|
||||
print(result)
|
||||
print(result.get_ran_qasm("qft3"))
|
||||
print(result.get_ran_qasm("qft4"))
|
||||
print(result.get_ran_qasm("qft5"))
|
||||
print(result.get_counts("qft3"))
|
||||
print(result.get_counts("qft4"))
|
||||
print(result.get_counts("qft5"))
|
||||
|
||||
|
||||
result = qp.execute(["qft3"], backend=backend,
|
||||
coupling_map=coupling_map, shots=1024, timeout=120)
|
||||
result = execute([qft3], backend='ibmq_5_tenerife', shots=1024).result()
|
||||
print(result)
|
||||
print(result.get_ran_qasm("qft3"))
|
||||
print(result.get_counts("qft3"))
|
||||
|
|
|
@ -1,161 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright 2017, IBM.
|
||||
#
|
||||
# This source code is licensed under the Apache License, Version 2.0 found in
|
||||
# the LICENSE.txt file in the root directory of this source tree.
|
||||
|
||||
"""
|
||||
Ripple adder example based on Cuccaro et al., quant-ph/0410184.
|
||||
|
||||
Note: if you have only cloned the Qiskit repository but not
|
||||
used `pip install`, the examples only work from the root directory.
|
||||
"""
|
||||
|
||||
import time
|
||||
from qiskit import QuantumProgram
|
||||
from qiskit import QuantumCircuit
|
||||
|
||||
import Qconfig
|
||||
|
||||
online_backend = "ibmq_qasm_simulator"
|
||||
local_backend = "local_qasm_simulator"
|
||||
|
||||
# Whether we have connection with API servers or not. If not, we only launch
|
||||
# jobs to the local simulator
|
||||
offline = False
|
||||
NUM_JOBS = 2 # TODO Parameterize
|
||||
n = 2
|
||||
QPS_SPECS = {
|
||||
"circuits": [
|
||||
{
|
||||
"name": "rippleadd",
|
||||
"quantum_registers": [
|
||||
{"name": "a",
|
||||
"size": n},
|
||||
{"name": "b",
|
||||
"size": n},
|
||||
{"name": "cin",
|
||||
"size": 1},
|
||||
{"name": "cout",
|
||||
"size": 1}
|
||||
],
|
||||
"classical_registers": [
|
||||
{"name": "ans",
|
||||
"size": n + 1},
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
qp = QuantumProgram(specs=QPS_SPECS)
|
||||
qc = qp.get_circuit("rippleadd")
|
||||
a = qp.get_quantum_register("a")
|
||||
b = qp.get_quantum_register("b")
|
||||
cin = qp.get_quantum_register("cin")
|
||||
cout = qp.get_quantum_register("cout")
|
||||
ans = qp.get_classical_register("ans")
|
||||
|
||||
|
||||
def majority(p, a, b, c):
|
||||
"""Majority gate."""
|
||||
p.cx(c, b)
|
||||
p.cx(c, a)
|
||||
p.ccx(a, b, c)
|
||||
|
||||
|
||||
def unmajority(p, a, b, c):
|
||||
"""Unmajority gate."""
|
||||
p.ccx(a, b, c)
|
||||
p.cx(c, a)
|
||||
p.cx(a, b)
|
||||
|
||||
|
||||
# Build a temporary subcircuit that adds a to b,
|
||||
# storing the result in b
|
||||
adder_subcircuit = QuantumCircuit(cin, a, b, cout)
|
||||
majority(adder_subcircuit, cin[0], b[0], a[0])
|
||||
for j in range(n - 1):
|
||||
majority(adder_subcircuit, a[j], b[j + 1], a[j + 1])
|
||||
adder_subcircuit.cx(a[n - 1], cout[0])
|
||||
for j in reversed(range(n - 1)):
|
||||
unmajority(adder_subcircuit, a[j], b[j + 1], a[j + 1])
|
||||
unmajority(adder_subcircuit, cin[0], b[0], a[0])
|
||||
|
||||
# Set the inputs to the adder
|
||||
qc.x(a[0]) # Set input a = 0...0001
|
||||
qc.x(b) # Set input b = 1...1111
|
||||
# Apply the adder
|
||||
qc += adder_subcircuit
|
||||
# Measure the output register in the computational basis
|
||||
for j in range(n):
|
||||
qc.measure(b[j], ans[j])
|
||||
qc.measure(cout[0], ans[n])
|
||||
|
||||
###############################################################
|
||||
# Set up the API and execute the program.
|
||||
###############################################################
|
||||
try:
|
||||
qp.set_api(Qconfig.APItoken, Qconfig.config["url"])
|
||||
except:
|
||||
offline = True
|
||||
print("""WARNING: There's no connection with IBMQuantumExperience servers.
|
||||
cannot test I/O intesive tasks, will only test CPU intensive tasks
|
||||
running the jobs in the local simulator""")
|
||||
|
||||
qobjs = []
|
||||
# Create online (so I/O bound) jobs if we have connetion or local (so CPU bound)
|
||||
# jobs otherwise
|
||||
if not offline:
|
||||
print("Creating %d online jobs..." % NUM_JOBS)
|
||||
for _ in range(0, NUM_JOBS):
|
||||
qobjs.append(qp.compile(["rippleadd"], backend=online_backend,
|
||||
coupling_map=None, shots=1024))
|
||||
|
||||
print("Creating %d local jobs..." % NUM_JOBS)
|
||||
# Create CPU intensive jobs
|
||||
for _ in range(0, NUM_JOBS):
|
||||
qobjs.append(qp.compile(["rippleadd"], backend=local_backend,
|
||||
coupling_map=None, shots=1024))
|
||||
|
||||
end = False
|
||||
def print_results_callback(results, error=None):
|
||||
"""This function will be called once all jobs have finished."""
|
||||
if error != None:
|
||||
print("There was an error executing the circuits!!: Error = {}".format(error))
|
||||
return
|
||||
|
||||
for result in results:
|
||||
print("result: {}".format(result))
|
||||
try:
|
||||
print(result.get_counts("rippleadd"))
|
||||
except Exception as ex:
|
||||
print("ERROR: {}".format(ex))
|
||||
|
||||
print("============")
|
||||
global end
|
||||
end = True
|
||||
|
||||
print("Running jobs asynchronously....")
|
||||
# This call is asynchronous, it won't block!
|
||||
qp.run_batch_async(qobjs, callback=print_results_callback)
|
||||
|
||||
# This will concurrently run while the jobs are being processed.
|
||||
for i in range(0, 100):
|
||||
print("Waitting for results...")
|
||||
time.sleep(0.5)
|
||||
if end:
|
||||
break
|
||||
|
||||
print("Running jobs synchronously...")
|
||||
results = qp.run_batch(qobjs)
|
||||
for result in results:
|
||||
print("result: {}".format(result))
|
||||
try:
|
||||
print(result.get_counts("rippleadd"))
|
||||
except Exception as ex:
|
||||
print("ERROR: {}".format(ex))
|
||||
|
||||
print("============")
|
||||
|
||||
print("Done")
|
|
@ -12,50 +12,30 @@ Note: if you have only cloned the Qiskit repository but not
|
|||
used `pip install`, the examples only work from the root directory.
|
||||
"""
|
||||
|
||||
from qiskit import QuantumProgram
|
||||
from qiskit import QuantumCircuit
|
||||
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
|
||||
from qiskit import execute, compile, register, get_backend
|
||||
|
||||
import Qconfig
|
||||
|
||||
###############################################################
|
||||
# Set the backend name and coupling map.
|
||||
###############################################################
|
||||
backend = "ibmq_qasm_simulator"
|
||||
coupling_map = {0: [1, 8], 1: [2, 9], 2: [3, 10], 3: [4, 11], 4: [5, 12],
|
||||
5: [6, 13], 6: [7, 14], 7: [15], 8: [9], 9: [10], 10: [11],
|
||||
11: [12], 12: [13], 13: [14], 14: [15]}
|
||||
backend = get_backend("local_qasm_simulator")
|
||||
coupling_map = [[0,1], [0, 8], [1, 2], [1, 9], [2, 3], [2, 10], [3, 4], [3, 11],
|
||||
[4, 5], [4, 12], [5, 6], [5, 13], [6, 7], [6, 14], [7, 15], [8, 9],
|
||||
[9, 10], [10, 11], [11, 12], [12, 13], [13, 14], [14, 15]]
|
||||
|
||||
###############################################################
|
||||
# Make a quantum program for the n-bit ripple adder.
|
||||
###############################################################
|
||||
n = 2
|
||||
|
||||
QPS_SPECS = {
|
||||
"circuits": [{
|
||||
"name": "rippleadd",
|
||||
"quantum_registers": [
|
||||
{"name": "a",
|
||||
"size": n},
|
||||
{"name": "b",
|
||||
"size": n},
|
||||
{"name": "cin",
|
||||
"size": 1},
|
||||
{"name": "cout",
|
||||
"size": 1}
|
||||
],
|
||||
"classical_registers": [
|
||||
{"name": "ans",
|
||||
"size": n + 1},
|
||||
]}]
|
||||
}
|
||||
|
||||
qp = QuantumProgram(specs=QPS_SPECS)
|
||||
qc = qp.get_circuit("rippleadd")
|
||||
a = qp.get_quantum_register("a")
|
||||
b = qp.get_quantum_register("b")
|
||||
cin = qp.get_quantum_register("cin")
|
||||
cout = qp.get_quantum_register("cout")
|
||||
ans = qp.get_classical_register("ans")
|
||||
a = QuantumRegister(n, "a")
|
||||
b = QuantumRegister(n, "b")
|
||||
cin = QuantumRegister(1, "cin")
|
||||
cout = QuantumRegister(1, "cout")
|
||||
ans = ClassicalRegister(n+1, "ans")
|
||||
qc = QuantumCircuit(a, b, cin, cout, ans, name="rippleadd")
|
||||
|
||||
|
||||
def majority(p, a, b, c):
|
||||
|
@ -94,23 +74,21 @@ for j in range(n):
|
|||
qc.measure(cout[0], ans[n])
|
||||
|
||||
###############################################################
|
||||
# Set up the API and execute the program.
|
||||
# execute the program.
|
||||
###############################################################
|
||||
qp.set_api(Qconfig.APItoken, Qconfig.config["url"])
|
||||
|
||||
# First version: not mapped
|
||||
result = qp.execute(["rippleadd"], backend=backend,
|
||||
coupling_map=None, shots=1024)
|
||||
job = execute(qc, backend=backend, coupling_map=None, shots=1024)
|
||||
result = job.result()
|
||||
print(result)
|
||||
print(result.get_counts("rippleadd"))
|
||||
|
||||
# Second version: mapped to 2x8 array coupling graph
|
||||
obj = qp.compile(["rippleadd"], backend=backend,
|
||||
coupling_map=coupling_map, shots=1024)
|
||||
result = qp.run(obj)
|
||||
qobj = compile(qc, backend=backend, coupling_map=coupling_map, shots=1024)
|
||||
job = backend.run(qobj)
|
||||
result = job.result()
|
||||
|
||||
print(result)
|
||||
print(result.get_ran_qasm("rippleadd"))
|
||||
print(result.get_counts("rippleadd"))
|
||||
|
||||
# Both versions should give the same distribution
|
||||
|
|
|
@ -12,46 +12,23 @@ Note: if you have only cloned the Qiskit repository but not
|
|||
used `pip install`, the examples only work from the root directory.
|
||||
"""
|
||||
|
||||
from qiskit import QuantumProgram
|
||||
import Qconfig
|
||||
|
||||
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
|
||||
from qiskit import execute
|
||||
|
||||
###############################################################
|
||||
# Set the backend name and coupling map.
|
||||
###############################################################
|
||||
backend = "ibmq_qasm_simulator"
|
||||
coupling_map = {0: [1, 2],
|
||||
1: [2],
|
||||
2: [],
|
||||
3: [2, 4],
|
||||
4: [2]}
|
||||
backend = "local_qasm_simulator"
|
||||
coupling_map = [[0, 1], [0, 2], [1, 2], [3, 2], [3, 4], [4, 2]]
|
||||
|
||||
###############################################################
|
||||
# Make a quantum program for quantum teleportation.
|
||||
###############################################################
|
||||
QPS_SPECS = {
|
||||
"circuits": [{
|
||||
"name": "teleport",
|
||||
"quantum_registers": [{
|
||||
"name": "q",
|
||||
"size": 3
|
||||
}],
|
||||
"classical_registers": [
|
||||
{"name": "c0",
|
||||
"size": 1},
|
||||
{"name": "c1",
|
||||
"size": 1},
|
||||
{"name": "c2",
|
||||
"size": 1},
|
||||
]}]
|
||||
}
|
||||
|
||||
qp = QuantumProgram(specs=QPS_SPECS)
|
||||
qc = qp.get_circuit("teleport")
|
||||
q = qp.get_quantum_register("q")
|
||||
c0 = qp.get_classical_register("c0")
|
||||
c1 = qp.get_classical_register("c1")
|
||||
c2 = qp.get_classical_register("c2")
|
||||
q = QuantumRegister(3, "q")
|
||||
c0 = ClassicalRegister(1, "c0")
|
||||
c1 = ClassicalRegister(1, "c1")
|
||||
c2 = ClassicalRegister(1, "c2")
|
||||
qc = QuantumCircuit(q, c0, c1, c2, name="teleport")
|
||||
|
||||
# Prepare an initial state
|
||||
qc.u3(0.3, 0.2, 0.1, q[0])
|
||||
|
@ -75,23 +52,18 @@ qc.x(q[2]).c_if(c1, 1)
|
|||
qc.measure(q[2], c2[0])
|
||||
|
||||
###############################################################
|
||||
# Set up the API and execute the program.
|
||||
###############################################################
|
||||
qp.set_api(Qconfig.APItoken, Qconfig.config["url"])
|
||||
|
||||
# Execute.
|
||||
# Experiment does not support feedback, so we use the simulator
|
||||
###############################################################
|
||||
|
||||
# First version: not mapped
|
||||
result = qp.execute(["teleport"], backend=backend,
|
||||
coupling_map=None, shots=1024)
|
||||
result = execute(qc, backend=backend, coupling_map=None, shots=1024).result()
|
||||
print(result)
|
||||
print(result.get_counts("teleport"))
|
||||
|
||||
# Second version: mapped to qx2 coupling graph
|
||||
result = qp.execute(["teleport"], backend=backend,
|
||||
coupling_map=coupling_map, shots=1024)
|
||||
result = execute(qc, backend=backend, coupling_map=coupling_map, shots=1024).result()
|
||||
print(result)
|
||||
print(result.get_ran_qasm("teleport"))
|
||||
print(result.get_counts("teleport"))
|
||||
|
||||
# Both versions should give the same distribution
|
||||
|
|
Loading…
Reference in New Issue