forked from Qiskit/qiskit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
using_qiskit_terra_level_0.py
94 lines (74 loc) · 2.94 KB
/
using_qiskit_terra_level_0.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# -*- coding: utf-8 -*-
# Copyright 2018, 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.
"""
Example showing how to use Qiskit-Terra at level 0 (novice).
This example shows the most basic way to user Terra. It builds some circuits
and runs them on both the Aer (local Qiskit provider) or IBMQ (remote IBMQ provider).
To control the compile parameters we have provided a compile function which can be used
as a level 1 user.
"""
import time
# Import the Qiskit modules
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister, QISKitError
from qiskit import execute, IBMQ, Aer
from qiskit.backends.ibmq import least_busy
try:
import Qconfig
IBMQ.enable_account(Qconfig.APItoken, Qconfig.config['url'])
except:
print("""WARNING: There's no connection with the API for remote backends.
Have you initialized a Qconfig.py file with your personal token?
For now, there's only access to local simulator backends...""")
try:
# Create a Quantum and Classical Register.
qubit_reg = QuantumRegister(2)
clbit_reg = ClassicalRegister(2)
# making first circuit: bell state
qc1 = QuantumCircuit(qubit_reg, clbit_reg)
qc1.h(qubit_reg[0])
qc1.cx(qubit_reg[0], qubit_reg[1])
qc1.measure(qubit_reg, clbit_reg)
# making another circuit: superpositions
qc2 = QuantumCircuit(qubit_reg, clbit_reg)
qc2.h(qubit_reg)
qc2.measure(qubit_reg, clbit_reg)
# setting up the backend
print("(AER Backends)")
print(Aer.backends())
# runing the job
job_sim = execute([qc1, qc2], Aer.get_backend('qasm_simulator'))
sim_result = job_sim.result()
# Show the results
print("simulation: ", sim_result)
print(sim_result.get_counts(qc1))
print(sim_result.get_counts(qc2))
# see a list of available remote backends
print("\n(IBMQ Backends)")
print(IBMQ.backends())
# Compile and run on a real device backend
try:
# select least busy available device and execute.
least_busy_device = least_busy(IBMQ.backends(simulator=False))
print("Running on current least busy device: ", least_busy_device)
# running the job
job_exp = execute([qc1, qc2], backend=least_busy_device, shots=1024, max_credits=10)
lapse = 0
interval = 10
while job_exp.status().name != 'DONE':
print('Status @ {} seconds'.format(interval * lapse))
print(job_exp.status())
time.sleep(interval)
lapse += 1
print(job_exp.status())
exp_result = job_exp.result()
# Show the results
print("experiment: ", exp_result)
print(exp_result.get_counts(qc1))
print(exp_result.get_counts(qc2))
except:
print("All devices are currently unavailable.")
except QISKitError as ex:
print('There was an error in the circuit!. Error = {}'.format(ex))