← Quantum Store · Guides · Lessons · Glossary · About · Türkçe

Qiskit course: real quantum programming · Lesson 1 / 6 · 10 min

Getting started with Qiskit: write your first circuit in Python

Install Qiskit, the most widely used quantum programming library, write the Bell circuit you built in the playground as Python code and draw it.

What is Qiskit?

Qiskit is an open-source Python library developed by IBM and the most widely used quantum programming tool in the world. It lets you build circuits, simulate them on your own computer and send the same circuits to IBM’s real quantum computers.

In the earlier courses you built circuits in the playground with the mouse. In this course you will write the same circuits as Python code, compute the results yourself and, by the end, run a circuit on a real quantum computer. Some Python helps, but every snippet is explained line by line.

Installation

All you need is a recent Python 3. To keep the project separate from other libraries, create a virtual environment first and install Qiskit inside it. The last line prints the installed version; if you see a number, you are ready.

python -m venv .venv
.venv\Scripts\activate          # macOS / Linux: source .venv/bin/activate
pip install qiskit
python -c "import qiskit; print(qiskit.__version__)"

Your first circuit: the Bell state

QuantumCircuit(2) creates an empty two-qubit circuit; both qubits start in |0⟩. Gates are lower-case methods: h, x, y, z, s and t act on one qubit, cx, cz and swap on two. Gates are applied in the order you write them.

In qc.cx(0, 1) the first number is the control and the second the target. measure_all() adds a measurement to every qubit and writes the results to a classical register called “meas”; we will use that name in the next lesson.

from qiskit import QuantumCircuit

qc = QuantumCircuit(2)   # two qubits, both start in |0⟩
qc.h(0)                  # put q0 into superposition
qc.cx(0, 1)              # q0 control, q1 target: entanglement
qc.measure_all()         # measure every qubit

print(qc.draw())

Drawing the circuit

print(qc.draw()) draws the circuit as text: each line is a qubit and q_0 is at the top. In a Jupyter notebook qc.draw("mpl") draws a nicer picture; it needs two more libraries, installed with pip install matplotlib pylatexenc.

The “Qiskit code for this circuit” button in the playground turns any circuit you build into code in exactly this form. Building a circuit with the mouse and then reading its code is the fastest way to learn Qiskit.

Try it yourself

Press the “Qiskit code for this circuit” button under the Bell circuit: the code is the same as in this lesson. Then add a gate and see which line appears in the code.

Open the circuit in your browser

The circuit playground is in Turkish. Button guide: “1000 kez ölç” = measure 1000 times, “Durum vektörünü göster” = show the state vector, “Bu devrenin Qiskit kodu” = this circuit’s Qiskit code, “Geri al” = undo, “Temizle” = clear. Click a gate in the circuit to remove it.

Key points

Test yourself

How do you write a CNOT with q0 as control and q1 as target in Qiskit?

Show the answer

qc.cx(0, 1). In the cx method the first number is the control and the second the target: qc.cx(0, 1).

Running a circuit: the Sampler and counts →

Share: X · LinkedIn