forked from Qiskit/qiskit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello_quantum.py
73 lines (60 loc) · 2.29 KB
/
hello_quantum.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
# This code is part of Qiskit.
#
# (C) Copyright IBM 2017, 2018.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.
"""Example used in the README. In this example a Bell state is made."""
# Import Qiskit
from qiskit import QuantumCircuit
from qiskit import execute, IBMQ, BasicAer
from qiskit.providers.ibmq import least_busy
# Create a Quantum Circuit
qc = QuantumCircuit(2, 2)
# Add a H gate on qubit 0, putting this qubit in superposition.
qc.h(0)
# Add a CX (CNOT) gate on control qubit 0 and target qubit 1, putting
# the qubits in a Bell state.
qc.cx(0, 1)
# Add a Measure gate to see the state.
qc.measure([0, 1], [0, 1])
# See a list of available local simulators
print("BasicAer backends: ", BasicAer.backends())
backend_sim = BasicAer.get_backend("qasm_simulator")
# Compile and run the Quantum circuit on a simulator backend
job_sim = execute(qc, backend_sim)
result_sim = job_sim.result()
# Show the results
print(result_sim.get_counts(qc))
# Authenticate for access to remote backends
try:
provider = IBMQ.load_account()
except:
print(
"""WARNING: No valid IBMQ credentials found on disk.
You must store your credentials using IBMQ.save_account(token, url).
For now, there's only access to local simulator backends..."""
)
exit(0)
# see a list of available remote backends
ibmq_backends = provider.backends()
print("Remote backends: ", ibmq_backends)
# Compile and run the Quantum Program on a real device backend
# select those with at least 2 qubits
try:
least_busy_device = least_busy(
provider.backends(filters=lambda x: x.configuration().n_qubits >= 2, simulator=False)
)
except:
print("All devices are currently unavailable.")
print("Running on current least busy device: ", least_busy_device)
# running the job
job_exp = execute(qc, least_busy_device, shots=1024, max_credits=10)
result_exp = job_exp.result()
# Show the results
print("Counts: ", result_exp.get_counts(qc))