mirror of https://github.com/Qiskit/qiskit.git
Examples update (#2256)
* Adding the level1 * fixing examples * level_2 * Bug fix * Fixing * make level 2 example richer
This commit is contained in:
parent
fb1937841f
commit
7e63af9823
|
@ -2,7 +2,6 @@ from qiskit import *
|
|||
|
||||
from qiskit.transpiler import PassManager
|
||||
from qiskit.transpiler.passes import CommutationAnalysis, CommutativeCancellation
|
||||
from qiskit.transpiler import transpile
|
||||
|
||||
qr = QuantumRegister(5, 'qr')
|
||||
circuit = QuantumCircuit(qr)
|
||||
|
@ -22,10 +21,6 @@ circuit.cx(qr[3], qr[2])
|
|||
print(circuit.draw())
|
||||
|
||||
pm = PassManager()
|
||||
|
||||
pm.append([CommutationAnalysis(), CommutativeCancellation()])
|
||||
|
||||
# TODO make it not needed to have a backend
|
||||
backend_device = BasicAer.get_backend('qasm_simulator')
|
||||
circuit = transpile(circuit, backend_device, pass_manager=pm)
|
||||
print(circuit.draw())
|
||||
new_circuit=pm.run(circuit)
|
||||
print(new_circuit.draw())
|
||||
|
|
|
@ -55,7 +55,7 @@ circuit.measure(qr[1], cr[1])
|
|||
circuit.measure(qr[2], cr[2])
|
||||
circuit.measure(qr[3], cr[3])
|
||||
|
||||
print(circuit.qasm())
|
||||
print(circuit.draw())
|
||||
|
||||
###############################################################
|
||||
# Execute on a simulator backend.
|
||||
|
|
|
@ -35,8 +35,7 @@ import pprint, time
|
|||
from qiskit import IBMQ, BasicAer
|
||||
from qiskit import QiskitError
|
||||
from qiskit.circuit import QuantumCircuit, ClassicalRegister, QuantumRegister
|
||||
from qiskit.compiler import transpile, assemble_circuits
|
||||
from qiskit.compiler import TranspileConfig, RunConfig
|
||||
from qiskit.compiler import transpile, assemble
|
||||
from qiskit.providers.ibmq import least_busy
|
||||
from qiskit.tools.monitor import job_monitor
|
||||
|
||||
|
@ -68,10 +67,6 @@ try:
|
|||
for backend in BasicAer.backends():
|
||||
print(backend.status())
|
||||
qasm_simulator = BasicAer.get_backend('qasm_simulator')
|
||||
print("(QASM Simulator configuration) ")
|
||||
pprint.pprint(qasm_simulator.configuration())
|
||||
print("(QASM Simulator properties) ")
|
||||
pprint.pprint(qasm_simulator.properties())
|
||||
|
||||
|
||||
# Compile and run the circuit on a real device backend
|
||||
|
@ -87,25 +82,21 @@ try:
|
|||
print("All devices are currently unavailable.")
|
||||
|
||||
print("Running on current least busy device: ", least_busy_device)
|
||||
print("(with configuration) ")
|
||||
pprint.pprint(least_busy_device.configuration())
|
||||
print("(with properties) ")
|
||||
pprint.pprint(least_busy_device.properties())
|
||||
|
||||
# Transpile the circuits to make them compatible with the experimental backend
|
||||
[qc1_new, qc2_new] = transpile(circuits=[qc1, qc2],
|
||||
transpile_config=TranspileConfig(backend=least_busy_device))
|
||||
[qc1_new, qc2_new] = transpile(circuits=[qc1, qc2], backend=least_busy_device)
|
||||
|
||||
print("Bell circuit before transpile:")
|
||||
print(qc1)
|
||||
print(qc1.draw())
|
||||
print("Bell circuit after transpile:")
|
||||
print(qc1_new)
|
||||
print(qc1_new.draw())
|
||||
print("Superposition circuit before transpile:")
|
||||
print(qc2)
|
||||
print(qc2.draw())
|
||||
print("Superposition circuit after transpile:")
|
||||
print(qc2_new)
|
||||
print(qc2_new.draw())
|
||||
|
||||
# Assemble the two circuits into a runnable qobj
|
||||
qobj = assemble_circuits([qc1_new, qc2_new], run_config=RunConfig(shots=1000))
|
||||
qobj = assemble([qc1_new, qc2_new], shots=1000)
|
||||
|
||||
# Running qobj on the simulator
|
||||
sim_job = qasm_simulator.run(qobj)
|
||||
|
|
|
@ -15,15 +15,31 @@
|
|||
"""
|
||||
Example showing how to use Qiskit at level 2 (advanced).
|
||||
|
||||
The order of the passes can determine the best way for a circuit to be complied. Here we
|
||||
make a simple circuit of 4 repeated CNOTs and show that the default pass is not as good
|
||||
as making a pass manager and telling it to start with CXCancellation.
|
||||
|
||||
|
||||
This example shows how an advanced user interacts with Terra.
|
||||
It builds some circuits and transpiles them with the pass_manager.
|
||||
"""
|
||||
|
||||
# choose a remote device
|
||||
from qiskit import IBMQ
|
||||
import pprint, time
|
||||
|
||||
# Import the Qiskit modules
|
||||
from qiskit import IBMQ, BasicAer
|
||||
from qiskit import QiskitError
|
||||
from qiskit.circuit import QuantumCircuit, ClassicalRegister, QuantumRegister
|
||||
from qiskit.extensions import SwapGate
|
||||
from qiskit.compiler import assemble
|
||||
from qiskit.providers.ibmq import least_busy
|
||||
from qiskit.tools.monitor import job_monitor
|
||||
|
||||
from qiskit.transpiler import PassManager
|
||||
from qiskit.transpiler import CouplingMap
|
||||
from qiskit.transpiler.passes import Unroller
|
||||
from qiskit.transpiler.passes import FullAncillaAllocation
|
||||
from qiskit.transpiler.passes import EnlargeWithAncilla
|
||||
from qiskit.transpiler.passes import TrivialLayout
|
||||
from qiskit.transpiler.passes import Decompose
|
||||
from qiskit.transpiler.passes import CXDirection
|
||||
from qiskit.transpiler.passes import LookaheadSwap
|
||||
|
||||
|
||||
try:
|
||||
IBMQ.load_accounts()
|
||||
|
@ -32,34 +48,94 @@ except:
|
|||
Have you initialized a file with your personal token?
|
||||
For now, there's only access to local simulator backends...""")
|
||||
|
||||
try:
|
||||
qubit_reg = QuantumRegister(4, name='q')
|
||||
clbit_reg = ClassicalRegister(4, name='c')
|
||||
|
||||
backend_device = IBMQ.get_backend('ibmqx4')
|
||||
# Making first circuit: superpositions
|
||||
qc1 = QuantumCircuit(qubit_reg, clbit_reg, name="bell")
|
||||
qc1.h(qubit_reg[0])
|
||||
qc1.cx(qubit_reg[0], qubit_reg[1])
|
||||
qc1.measure(qubit_reg, clbit_reg)
|
||||
|
||||
# 0. build circuit
|
||||
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
|
||||
q = QuantumRegister(2)
|
||||
c = ClassicalRegister(2)
|
||||
circ = QuantumCircuit(q, c)
|
||||
circ.cx(q[0], q[1])
|
||||
circ.cx(q[0], q[1])
|
||||
circ.cx(q[0], q[1])
|
||||
circ.cx(q[0], q[1])
|
||||
circ.measure(q, c)
|
||||
# Making another circuit: GHZ State
|
||||
qc2 = QuantumCircuit(qubit_reg, clbit_reg, name="superposition")
|
||||
qc2.h(qubit_reg)
|
||||
qc2.cx(qubit_reg[0], qubit_reg[1])
|
||||
qc2.cx(qubit_reg[0], qubit_reg[2])
|
||||
qc2.cx(qubit_reg[0], qubit_reg[3])
|
||||
qc2.measure(qubit_reg, clbit_reg)
|
||||
|
||||
# draw circuit
|
||||
print("orginal circuit")
|
||||
print(circ.draw())
|
||||
# Setting up the backend
|
||||
print("(Aer Backends)")
|
||||
for backend in BasicAer.backends():
|
||||
print(backend.status())
|
||||
qasm_simulator = BasicAer.get_backend('qasm_simulator')
|
||||
|
||||
# 1. standard compile -- standard qiskit passes, when no PassManager given
|
||||
from qiskit import transpiler
|
||||
circuit1 = transpiler.transpile(circ, backend_device)
|
||||
print("circuit after standard pass manager")
|
||||
print(circuit1.draw())
|
||||
|
||||
# 2. custom compile -- customize PassManager to run specific circuit transformations
|
||||
from qiskit.transpiler.passes import CXCancellation
|
||||
pm = transpiler.PassManager()
|
||||
pm.append(CXCancellation())
|
||||
circuit2 = transpiler.transpile(circ, backend_device, pass_manager=pm)
|
||||
print("circuit after custom pass manager")
|
||||
print(circuit2.draw())
|
||||
# Compile and run the circuit on a real device backend
|
||||
# See a list of available remote backends
|
||||
print("\n(IBMQ Backends)")
|
||||
for backend in IBMQ.backends():
|
||||
print(backend.status())
|
||||
|
||||
try:
|
||||
# select least busy available device and execute.
|
||||
least_busy_device = least_busy(IBMQ.backends(simulator=False))
|
||||
except:
|
||||
print("All devices are currently unavailable.")
|
||||
|
||||
print("Running on current least busy device: ", least_busy_device)
|
||||
|
||||
|
||||
# making a pass manager to compile the circuits
|
||||
coupling_map = CouplingMap(least_busy_device.configuration().coupling_map)
|
||||
print("coupling map: ", coupling_map)
|
||||
|
||||
pm = PassManager()
|
||||
pm.append(TrivialLayout(coupling_map))
|
||||
pm.append(FullAncillaAllocation(coupling_map))
|
||||
pm.append(EnlargeWithAncilla())
|
||||
pm.append(LookaheadSwap(coupling_map))
|
||||
pm.append(Decompose(SwapGate))
|
||||
pm.append(CXDirection(coupling_map))
|
||||
pm.append(Unroller(['u1', 'u2', 'u3', 'id', 'cx']))
|
||||
qc1_new = pm.run(qc1)
|
||||
qc2_new = pm.run(qc2)
|
||||
|
||||
print("Bell circuit before passes:")
|
||||
print(qc1.draw())
|
||||
print("Bell circuit after passes:")
|
||||
print(qc1_new.draw())
|
||||
print("Superposition circuit before passes:")
|
||||
print(qc2.draw())
|
||||
print("Superposition circuit after passes:")
|
||||
print(qc2_new.draw())
|
||||
|
||||
# Assemble the two circuits into a runnable qobj
|
||||
qobj = assemble([qc1_new, qc2_new], shots=1000)
|
||||
|
||||
# Running qobj on the simulator
|
||||
print("Running on simulator:")
|
||||
sim_job = qasm_simulator.run(qobj)
|
||||
|
||||
# Getting the result
|
||||
sim_result=sim_job.result()
|
||||
|
||||
# Show the results
|
||||
print(sim_result.get_counts(qc1))
|
||||
print(sim_result.get_counts(qc2))
|
||||
|
||||
# Running the job.
|
||||
print("Running on device:")
|
||||
exp_job = least_busy_device.run(qobj)
|
||||
|
||||
job_monitor(exp_job)
|
||||
exp_result = exp_job.result()
|
||||
|
||||
# Show the results
|
||||
print(exp_result.get_counts(qc1))
|
||||
print(exp_result.get_counts(qc2))
|
||||
|
||||
except QiskitError as ex:
|
||||
print('There was an error in the circuit!. Error = {}'.format(ex))
|
||||
|
|
Loading…
Reference in New Issue