forked from Qiskit/qiskit
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
So Diego can see Fix for the quantum program with new backends Fixing the tests Small changes Linting Adding the qp to the compile section for completeness Doc changes naming Small changes Linting Small changes Moving the example WIP Backend example (Qiskit#372) * Fix for the quantum program with new backends * Fixing the tests * Small changes * Linting * Update tests using coupling_map as list, add test Update the test in order to specify the coupling_maps as lists, due to recent changes. Add a `test_compile_coupling_map_as_dict` that still uses the dict format, for backwards compatibility during the deprecation. * Update coupling_map as list related docstrings Along with other fixes to docstrings and tests. * Disable travis osx build temporarily (Qiskit#374) Temporarily disable the osx build on Travis, as they are having a backlog that might take some time to recover: https://www.traviscistatus.com/incidents/qkqy13yk55q9 * adding a missing coupling in test_quantumprogram
- Loading branch information
1 parent
90eb2dc
commit 05ac499
Showing
1 changed file
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
""" | ||
Example used in the README. In this example a Bell state is made. | ||
Note: if you have only cloned the QISKit repository but not | ||
used `pip install`, the examples only work from the root directory. | ||
""" | ||
|
||
# Import the QISKit SDK | ||
import qiskit | ||
# Import the IBM Q Experience API | ||
from IBMQuantumExperience import IBMQuantumExperience | ||
# XXX ideally rather than import IBMQuantumExperience you do sth like: | ||
# api = qiskit.remote() | ||
|
||
# Authenticate for access to remote backends | ||
# XXX ideally instead of import QConfig we use some localised configuration (windows: registry | ||
# unix: dotfile, etc) | ||
try: | ||
import Qconfig | ||
api = IBMQuantumExperience(token=Qconfig.APItoken, | ||
config={'url': Qconfig.config['url']}) | ||
remote_backends = qiskit.backends.discover_remote_backends(api) | ||
except: | ||
print("""WARNING: There's no connection with IBMQuantumExperience servers. | ||
Have you initialized a Qconfig.py file with your personal token? | ||
For now, there's only access to local simulator backends...""") | ||
local_backends = qiskit.backends.discover_local_backends() | ||
|
||
try: | ||
# Create a Quantum Register called "qr" with 2 qubits. | ||
qr = qiskit.QuantumRegister("qr", 2) | ||
# Create a Classical Register called "cr" with 2 bits. | ||
cr = qiskit.ClassicalRegister("cr", 2) | ||
|
||
# Create a Quantum Circuit called involving "qr" and "cr" | ||
qc1 = qiskit.QuantumCircuit(qr, cr) | ||
# Add a H gate on qubit 0, putting this qubit in superposition. | ||
qc1.h(qr[0]) | ||
# Add a CX (CNOT) gate on control qubit 0 and target qubit 1, putting | ||
# the qubits in a Bell state. | ||
qc1.cx(qr[0], qr[1]) | ||
# Add a Measure gate to see the state. | ||
qc1.measure(qr, cr) | ||
|
||
# making another circuit of all superpositions | ||
qc2 = qiskit.QuantumCircuit(qr, cr) | ||
qc2.h(qr) | ||
qc2.measure(qr, cr) | ||
|
||
#setting up the backend | ||
print("(Local Backends)") | ||
for backend in local_backends: | ||
print(backend) | ||
my_backend = qiskit.backends.get_backend_instance('local_qasm_simulator') | ||
# ideally this should be | ||
# my_backend = qiskit.backends.get_backend_instance(filter on local and qasm simulator) | ||
# backend methods that exist are .config, .status .calibration and .run and .parameters | ||
# new method is .validate which returns a ticket that goes though some simulaitons | ||
|
||
#compiling the job | ||
qp = qiskit.QuantumProgram() | ||
qp.add_circuit("bell", qc1) | ||
qp.add_circuit("superposition", qc2) | ||
circuit_runs = ["bell","superposition"] | ||
qobj = qp.compile(circuit_runs, backend='local_qasm_simulator', shots=1024, seed=1) | ||
q_job = qiskit.QuantumJob(qobj, preformatted=True) | ||
# I am not convince the q_job is the correct class i would make a qobj class | ||
# ideally this should be qobj = qiskit.compile([qc],config) or qobj = QuantumObject([qc]) then qobj.compile | ||
# set the congig | ||
# with the coupling layout, qubit layout, gate set etc | ||
# qobj = qiskit.compile([qc],config) | ||
|
||
|
||
#runing the job | ||
sim_result = my_backend.run(q_job) | ||
#ideally this would be | ||
#job = my_backend.run(qobj) | ||
#job.status | ||
#sim_result=job.results | ||
# the job is a new object that runs when it does and i dont wait for it to finish and can get results later | ||
# other job methods are job.abort | ||
|
||
|
||
# Show the results | ||
print("simulation: ", sim_result) | ||
print(sim_result.get_counts("bell")) | ||
print(sim_result.get_counts("superposition")) | ||
|
||
# Compile and run the Quantum Program on a real device backend | ||
if remote_backends: | ||
|
||
|
||
# see a list of available remote backends | ||
print("\n(Remote Backends)") | ||
for backend in remote_backends: | ||
exp_backend = qiskit.backends.get_backend_instance(backend) | ||
backend_status = exp_backend.status | ||
print(backend, backend_status) | ||
|
||
# select least busy available device and execute | ||
device_status = [api.backend_status(backend) | ||
for backend in remote_backends if "simulator" not in backend] | ||
best_device = min([x for x in device_status if x['available']==True], | ||
key=lambda x:x['pending_jobs']) | ||
print("Running on current least busy device: ", best_device['backend']) | ||
my_backend = qiskit.backends.get_backend_instance(best_device['backend']) | ||
# this gets replaced by | ||
# my_backend = qiskit.backends.get_backend_instance(filter remote, device, smallest queue) | ||
|
||
#compiling the job | ||
qp = qiskit.QuantumProgram() | ||
qp.add_circuit("bell", qc1) | ||
qp.add_circuit("superposition", qc2) | ||
circuit_runs = ["bell","superposition"] | ||
qobj = qp.compile(circuit_runs, backend=best_device['backend'], shots=1024, seed=1) | ||
wait = 5 | ||
timeout = 300 | ||
q_job = qiskit.QuantumJob(qobj, preformatted=True, resources={ | ||
'max_credits': qobj['config']['max_credits'], 'wait': wait, | ||
'timeout': timeout}) | ||
# I am not convince the q_job is the correct class i would make a qobj class | ||
# ideally this should be qobj = qiskit.compile([qc],config) or qobj = QuantumObject([qc]) then qobj.compile | ||
|
||
#runing the job | ||
exp_result = my_backend.run(q_job) | ||
#ideally this would be | ||
#job = my_backend.run(qobj) | ||
#job.status | ||
#sim_result=job.results | ||
# the job is a new object that runs when it does and i dont wait for it to finish and can get results later | ||
# other job methods are job.abort | ||
#job = my_backend.run(qobj) | ||
#job.status() | ||
#exp_result=job.results() | ||
|
||
# Show the results | ||
print("experiment: ", exp_result) | ||
print(exp_result.get_counts("bell")) | ||
print(exp_result.get_counts("superposition")) | ||
|
||
except qiskit.QISKitError as ex: | ||
print('There was an error in the circuit!. Error = {}'.format(ex)) |