Getting Started¶
The starting point for writing code is the QuantumCircuit
.
A circuit (or score if you are coming from the Quantum Experience) are
collections of ClassicalRegister
objects,
QuantumRegister
objects and
gates
. Through the
top-level functions, the circuits can be
sent to remote quantum devices or local simulator backends and collect the
results for further analysis.
To compose and run a circuit on a simulator, which is distributed with this project, one can do,
# Import the QISKit SDK
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister
from qiskit import available_backends, execute
# Create a Quantum Register with 2 qubits.
q = QuantumRegister(2)
# Create a Classical Register with 2 bits.
c = ClassicalRegister(2)
# Create a Quantum Circuit
qc = QuantumCircuit(q, c)
# Add a H gate on qubit 0, putting this qubit in superposition.
qc.h(q[0])
# Add a CX (CNOT) gate on control qubit 0 and target qubit 1, putting
# the qubits in a Bell state.
qc.cx(q[0], q[1])
# Add a Measure gate to see the state.
qc.measure(q, c)
# See a list of available local simulators
print("Local backends: ", available_backends({'local': True}))
# Compile and run the Quantum circuit on a simulator backend
job_sim = execute(qc, "local_qasm_simulator")
sim_result = job_sim.result()
# Show the results
print("simulation: ", sim_result)
print(sim_result.get_counts(qc))
The get_counts()
method outputs a dictionary of
state:counts
pairs;
{'00': 531, '11': 493}
Quantum Chips¶
You can execute your QASM circuits on a real chip by using the IBM Q experience (QX) cloud platform. Currently through QX you can use the following chips:
ibmqx4
: 5-qubit backendibmqx5
: 16-qubit backend
For chip details and realtime information about availability visit the IBM Q experience backend information and the IBM Q experience devices page.
Example Real Chip Backend¶
The following code is an example of how to execute a Quantum Program on a real Quantum device:
# Import the QISKit SDK
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister
from qiskit import execute, register
# Set your API Token.
# You can get it from https://quantumexperience.ng.bluemix.net/qx/account,
# looking for "Personal Access Token" section.
QX_TOKEN = "API_TOKEN"
QX_URL = "https://quantumexperience.ng.bluemix.net/api"
# Authenticate with the IBM Q API in order to use online devices.
# You need the API Token and the QX URL.
register(QX_TOKEN, QX_URL)
# Create a Quantum Register with 2 qubits.
q = QuantumRegister(2)
# Create a Classical Register with 2 bits.
c = ClassicalRegister(2)
# Create a Quantum Circuit
qc = QuantumCircuit(q, c)
# Add a H gate on qubit 0, putting this qubit in superposition.
qc.h(q[0])
# Add a CX (CNOT) gate on control qubit 0 and target qubit 1, putting
# the qubits in a Bell state.
qc.cx(q[0], q[1])
# Add a Measure gate to see the state.
qc.measure(q, c)
# Compile and run the Quantum Program on a real device backend
job_exp = execute(qc, 'ibmqx4', shots=1024, max_credits=10)
result = job_exp.result()
# Show the results
print(result)
print(result.get_data())
Example Real Chip Backend using IBMQ¶
If you have access to the IBM Q features, the following code can be used for executing the same example as described on the previous section, but using the IBM Q features:
# Import the QISKit SDK
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister
from qiskit import execute, register
# Set your API Token and credentials.
# You can get it from https://quantumexperience.ng.bluemix.net/qx/account,
# looking for "Personal Access Token" section.
QX_TOKEN = "API_TOKEN"
QX_URL = "https://quantumexperience.ng.bluemix.net/api"
QX_HUB = "MY_HUB"
QX_GROUP = "MY_GROUP"
QX_PROJECT = "MY_PROJECT"
# Authenticate with the IBM Q API in order to use online devices.
# You need the API Token and the QX URL.
register(QX_TOKEN, QX_URL,
hub=QX_HUB,
group=QX_GROUP,
project=QX_PROJECT)
# Create a Quantum Register with 2 qubits.
q = QuantumRegister(2)
# Create a Classical Register with 2 bits.
c = ClassicalRegister(2)
# Create a Quantum Circuit
qc = QuantumCircuit(q, c)
# Add a H gate on qubit 0, putting this qubit in superposition.
qc.h(q[0])
# Add a CX (CNOT) gate on control qubit 0 and target qubit 1, putting
# the qubits in a Bell state.
qc.cx(q[0], q[1])
# Add a Measure gate to see the state.
qc.measure(q, c)
# Compile and run the Quantum Program on a real device backend
job_exp = execute(qc, 'ibmqx4', shots=1024, max_credits=10)
result = job_exp.result()
# Show the results
print(result)
print(result.get_data())
Please check the Installation 3. Configure your API token and QE credentials section for more details on how to setup your IBM Q credentials.
Using the HPC online backend¶
The ibmq_qasm_simulator_hpc
online backend has the following configurable
parameters:
multi_shot_optimization
: boolean (True or False)omp_num_threads
: integer between 1 and 16.
The parameters can be specified to qiskit.compile()
and
qiskit.execute()
via the hpc
parameter. For example:
qiskit.compile(circuits,
backend=backend,
shots=shots,
seed=88,
hpc={
'multi_shot_optimization': True,
'omp_num_threads': 16
})
If the ibmq_qasm_simulator_hpc
backend is used and the hpc
parameter
is not specified, the following values will be used by default:
hpc={
'multi_shot_optimization': True,
'omp_num_threads': 16
}
Please note that these parameters must only be used for the
ibmq_qasm_simulator_hpc
, and will be reset to None along with emitting
a warning by the SDK if used with another backend.
Project Organization¶
Python example programs can be found in the examples directory, and test scripts are located in test. The qiskit directory is the main module of the SDK.