The IBM Quantum Challenge Fall 2022 is coming. It is a virtual event, scheduled for 8 days, from 9:00 AM Nov 11th to 9:00 AM Nov 18th, 2022, U.S. Eastern time. An event like this can be a good opportunity. For learning. Or, even as a simple nudge for you to get started if you have been meaning to learn quantum computing but putting it off.
IBM Quantum has an SDK for quantum computation, called Qiskit. It is a Python-based library. This post will give you a brief introduction on how to get started with Qiskit, if you are new to Qiskit. (There are many resources on quantum computing, in general, and Qiskit, in particular. The readers are encouraged to consult other resources as well.)
Prerequisites
- Basic knowledge of Python, and
- A working installation of Python 3.7 or later.
A Qiskit Demo Project Setup
Let's create a new venv environment for our new project:
$ python3 --version
Python 3.10.6
$ mkdir qiskit-demo && cd $_
$ python3 -m venv venv
$ . venv/bin/activate
(venv) $
Next, let's
pip install qiskit
and some other (optional) packages
for the purposes of this demo.
(Within the same venv. We will just use the same prompt $
.)
$ python --version
Python 3.10.6
$ python -m pip install -U pip pep8 wheel
$ python -m pip install qiskit
Now, a quick sanity check:
$ Python
Python 3.10.6 (main, Nov 2 2022, 18:53:38) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import qiskit
>>> dir(qiskit)
['Aer', 'AerWrapper', 'AncillaRegister', 'BasicAer', 'ClassicalRegister', 'IBMQ', 'IBMQWrapper', 'MissingOptionalLibraryError', 'QiskitError', 'QiskitVersion', 'QuantumCircuit', 'QuantumRegister', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__qiskit_version__', '__spec__', '__version__', '_accelerate', '_config', '_user_config', 'assemble', 'assembler', 'circuit', 'compiler', 'converters', 'dagcircuit', 'exceptions', 'execute', 'execute_function', 'extensions', 'namespace', 'new_meta_path_finder', 'pkgutil', 'providers', 'pulse', 'qasm', 'qiskit', 'qobj', 'quantum_info', 'result', 'schedule', 'scheduler', 'sequence', 'synthesis', 'sys', 'tools', 'transpile', 'transpiler', 'user_config', 'utils', 'version', 'warnings']
>>> qiskit.__version__
'0.22.2'
OK, everything seems to be working. Moving on...
IBMId Account
In order to be able to use the IBM's quantum computers on their cloud, e.g., through the IBM Quantum Experience website, you will need a (free) access key. Let's create a new IBM account first, if you don't have one already.
Next, go to the IBM Quantum site, and log in with your (new) IBMId. You will find your first API token automatically created for you. You can "save" the auth token in your local environment as follows:
>>> import qiskit
>>> qiskit.IBMQ.save_account('my auth token')
Hello Quantum World!
You cannot really print "hello world" in quantum computation, but let's try running a simple Qiskit script, in REPL:
>>> import qiskit as qi
>>>
>>> qr = qi.QuantumRegister(2)
>>> cr = qi.ClassicalRegister(2)
>>>
>>> circ = qi.QuantumCircuit(qr, cr)
>>> circ.draw()
q0_0:
q0_1:
c1_0:
c1_1:
We create two qubits and two classical bits (or, more precisely "registers")
and name them qr
and cr
, respectively.
The following line qi.QuantumCircuit(qr, cr)
then creates a "quantum circuit"
and assigns it to a variable circ
.
The QuantumCircuit.draw()
statement "draws" the circuit on the terminal.
If you use Jupyter Notebook or something comparable,
it will generate a fancier graph.
Not much interesting, but we did use the Qiskit API. For the first time. :)
(Note: If you are new to quantum computing, you are not expected to understand this code. The goal of this lesson is to give you a little taste of quantum programming process using Qiskit on the IBM Cloud.)
A Sample Code
Just a little bit more interesting program.
import qiskit as qi
qr = qi.QuantumRegister(2)
cr = qi.ClassicalRegister(2)
circ = qi.QuantumCircuit(qr, cr)
circ.draw()
This part is the same as our first "hello quantum world" program.
We next add a Hadamard gate to the first qubit.
(h
for Hadamard.)
circ.h(qr[0])
circ.draw()
A Hadamard gate is one of the predefined quantum logic gates, and it is commonly used to "de-sharpen" the quantum state of a given qubit. At this point, the circuit looks like this:
┌───┐
q1_0: ┤ H ├
└───┘
q1_1: ─────
c1_0: ═════
c1_1: ═════
Then, we create a CNOT gate ("Controlled Not") with the first and second qubits as input. This is the "main operation" of this program.
circ.cx(qr[0], qr[1])
circ.draw()
Here's a circuit drawing:
┌───┐
q1_0: ┤ H ├──■──
└───┘┌─┴─┐
q1_1: ─────┤ X ├
└───┘
c1_0: ══════════
c1_1: ══════════
To see the "result",
we will need to take a measurement.
(That's what Quantum Theory prescribes.)
We use the QuantumCircuit.measure()
method:
circ.measure(qr, cr)
circ.draw()
The final circuit looks like this:
┌───┐ ┌─┐
q1_0: ┤ H ├──■──┤M├───
└───┘┌─┴─┐└╥┘┌─┐
q1_1: ─────┤ X ├─╫─┤M├
└───┘ ║ └╥┘
c1_0: ═══════════╩══╬═
║
c1_1: ══════════════╩═
This sample code is available on GitLab:
Running
Now that we have a complete (albeit trivial) circuit, let's try running it.
Simulator
We run it on the simulator first.
sim = qi.Aer.get_backend('qasm_simulator')
result = qi.execute(circ, backend=sim).result()
counts = result.get_counts()
print(counts)
Here's a sample result:
{'00': 516, '11': 508}
Out of 1024 measurements,
00
came out 516 times
and 11
came out the remaining 508 times.
(Note: It will require some basic understanding of quantum mechanics
to know why this is the case, but the focus of this post is
to help you get started with Qiskit programming.)
Real Quantum Computer
Since the circuit seems to work without any obvious issues, let's try running it on a real quantum computer. First, let's load the previously saved auth credential.
>>> qi.IBMQ.load_account()
# <AccountProvider for IBMQ(hub='ibm-q', group='open', project='main')>
Then we submit a job to the queue for processing.
provider = qi.IBMQ.get_provider('ibm-q')
qcomp = provider.get_backend('ibmq_lima')
job = qi.execute(circ, backend=qcomp)
Don't worry about the details.
We call qiskit.execute()
to submit a new job with a given circuit as an argument.
Monitoring
You can monitor the job status as follows:
>>> from qiskit.tools.monitor import job_monitor
>>> job_monitor(job)
Job Status: job is queued (475)
Job Status: job has successfully run
Histogram
Here's a sample result from this particular run.
(Note: The 01
and 10
occurrences are due to the noise from the quantum circuits.)
What's Next?
Now, if you were motivated enough to follow the basic setup steps in this lesson, then I recommend you head over to the IBM Quantum Challenge Fall 2022 registration site, and take a quantum leap.
It is not about the "challenge". Nor does it have any external significance. But, this kind of random event can motivate you, and it might just change your life. :)
We just published a new article on Quantum Computing. If you are interested, please check it out. This article includes a brief "primer" to Quantum Computing for people who are just starting out.