-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* removed relative import warning from examples * Cleaner examples
- Loading branch information
1 parent
4164a2c
commit e49ab48
Showing
10 changed files
with
144 additions
and
150 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
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
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
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 |
---|---|---|
@@ -1,59 +1,78 @@ | ||
""" | ||
Example used in the readme. In this example a Bell state is made | ||
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 sys | ||
import os | ||
from pprint import pprint | ||
# so we need a relative position from this file path. | ||
# TODO: Relative imports for intra-package imports are highly discouraged. | ||
# http://stackoverflow.com/a/7506006 | ||
sys.path.append(os.path.join(os.path.dirname(__file__), '../..')) | ||
from qiskit import QuantumProgram, QISKitError, RegisterSizeError | ||
|
||
# Create a QuantumProgram object instance. | ||
Q_program = QuantumProgram() | ||
|
||
# Import the QISKit SDK | ||
import qiskit | ||
# Import the IBMQ Experience API | ||
from IBMQuantumExperience import IBMQuantumExperience | ||
|
||
# Authenticate for access to remote backends | ||
try: | ||
import Qconfig | ||
Q_program.set_api(Qconfig.APItoken, Qconfig.config["url"], verify=False, | ||
hub=Qconfig.config["hub"], | ||
group=Qconfig.config["group"], | ||
project=Qconfig.config["project"]) | ||
api = IBMQuantumExperience(token=Qconfig.APItoken, | ||
config={'url': Qconfig.config['url']}) | ||
remote_backends = qiskit.backends.discover_remote_backends(api) | ||
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""") | ||
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() | ||
|
||
print("The backends available for use are:") | ||
pprint(Q_program.available_backends()) | ||
print("\n") | ||
backend = 'local_qasm_simulator' | ||
try: | ||
# Create a Quantum Register called "qr" with 2 qubits. | ||
qr = Q_program.create_quantum_register("qr", 2) | ||
qr = qiskit.QuantumRegister("qr", 2) | ||
# Create a Classical Register called "cr" with 2 bits. | ||
cr = Q_program.create_classical_register("cr", 2) | ||
# Create a Quantum Circuit called "qc". involving the Quantum Register "qr" | ||
# and the Classical Register "cr". | ||
qc = Q_program.create_circuit("bell", [qr], [cr]) | ||
cr = qiskit.ClassicalRegister("cr", 2) | ||
# Create a Quantum Circuit called involving "qr" and "cr" | ||
qc = qiskit.QuantumCircuit(qr, cr) | ||
|
||
# Add the H gate in the Qubit 0, putting this qubit in superposition. | ||
# Add a H gate on qubit 0, putting this qubit in superposition. | ||
qc.h(qr[0]) | ||
# Add the CX gate on control qubit 0 and target qubit 1, putting | ||
# the qubits in a Bell state | ||
# Add a CX (CNOT) gate on control qubit 0 and target qubit 1, putting | ||
# the qubits in a Bell state. | ||
qc.cx(qr[0], qr[1]) | ||
|
||
# Add a Measure gate to see the state. | ||
qc.measure(qr, cr) | ||
|
||
# Compile and execute the Quantum Program in the local_qasm_simulator. | ||
result = Q_program.execute(["bell"], backend=backend, shots=1024, seed=1) | ||
# Create a Quantum Program for execution | ||
qp = qiskit.QuantumProgram() | ||
# Add the circuit you created to it, and call it the "bell" circuit. | ||
# (You can add multiple circuits to the same program, for batch execution) | ||
qp.add_circuit("bell", qc) | ||
|
||
# Compile and run the Quantum Program on a simulator backend | ||
print("(Local Backends)") | ||
for backend in local_backends: | ||
print(backend) | ||
sim_result = qp.execute("bell", backend='local_qasm_simulator', shots=1024, seed=1) | ||
|
||
# Show the results | ||
print("simulation: ", sim_result) | ||
print(sim_result.get_counts("bell")) | ||
|
||
# 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: | ||
backend_status = api.backend_status(backend) | ||
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']) | ||
exp_result = qp.execute("bell", backend=best_device['backend'], shots=1024, timeout=300) | ||
|
||
# Show the results. | ||
print(result) | ||
print(result.get_data("bell")) | ||
# Show the results | ||
print("experiment: ", exp_result) | ||
print(exp_result.get_counts("bell")) | ||
|
||
except QISKitError as ex: | ||
except qiskit.QISKitError as ex: | ||
print('There was an error in the circuit!. Error = {}'.format(ex)) | ||
except RegisterSizeError as ex: | ||
print('Error in the number of registers!. Error = {}'.format(ex)) |
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
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
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
Oops, something went wrong.