Quantum Computing Master Course — 143. Qiskit Circuit Basics
Lesson 143: Qiskit Circuit Basics
Smartyfy Quantum Computing Master Course
Objectives and prerequisites
Read and construct a small Qiskit circuit, distinguish quantum instructions from execution, and keep an unmeasured preparation separate from its measured copy. Prerequisites are the standard-library simulator, circuit diagrams, and bit ordering. The official documentation identified Qiskit SDK 2.5.2 as its current stable reference when checked on 10 October 2026.
Execution status: Qiskit was not installed in the authoring environment, and no package installation was performed. The SDK snippets in this lesson were checked against official documentation but were not executed here. Their ideal mathematical behavior was independently checked with the local simulator. No account, service connection, API key, or hardware call is part of the example.
A circuit object is a description
QuantumCircuit stores quantum and classical bits together with an ordered sequence of instructions. Appending h(0) describes a Hadamard on qubit index zero; appending cx(0,1) describes a controlled X with control zero and target one. Constructing these objects does not itself submit a job or cause a physical experiment.
The distinction is useful for reproducibility. First define the intended circuit, then choose how to inspect, simulate, sample, compile, or execute it. A circuit diagram displays its structure, while a statevector calculation produces a mathematical state under a simulation model. A measurement instruction declares a quantum-to-classical operation; it does not supply random counts until an execution or sampling method evaluates it.
Qiskit's SDK is an open-source programming layer distinct from managed hardware services. The local examples here use only circuit and statevector functionality. Importing a hardware service class or loading account credentials would add capabilities and requirements that this exercise does not need.
Declare the same ordering used in our simulator
Qubit q0 is the least-significant bit of a computational-basis integer. A two-qubit state string is q1q0, with q1 on the left. The default circuit diagram usually draws q0 on the top wire, so visual vertical order is not the same thing as left-to-right bit-string significance.
For the sequence H on q0 followed by CX(0,1), the state begins at |00⟩, becomes (|00⟩ + |01⟩)/√2, and then becomes (|00⟩ + |11⟩)/√2. The ordering agrees with the local simulator. A different diagram orientation does not change which qubit an instruction names.
| Expression | Meaning | What it does not do |
|---|---|---|
| QuantumCircuit(2) | Create a two-qubit circuit description | Allocate remote hardware |
| h(0) | Append Hadamard on q0 | Return a measurement outcome |
| cx(0,1) | Append CNOT from q0 to q1 | Reverse roles based on wire position |
| draw(output='text') | Render a textual diagram | Simulate the circuit |
Documentation-checked circuit example
import qiskit
from qiskit import QuantumCircuit
print('Installed Qiskit:', qiskit.__version__)
bell = QuantumCircuit(2, name='bell_preparation')
bell.h(0)
bell.cx(0, 1)
measured = bell.copy()
measured.measure_all()
assert bell.num_clbits == 0
assert measured.num_clbits == 2
print(bell.draw(output='text'))
print(measured.draw(output='text'))
print('Preparation operations:', bell.count_ops())
The expected preparation description contains one h instruction and one cx instruction. The code asks the installed SDK to report its version rather than assuming the reader's environment matches the documentation check. The exact text layout of a diagram is presentation output and can change across versions without changing the circuit's mathematical meaning.
The unmeasured circuit has no classical bits. Calling measure_all on the copy adds measurement instructions and, by default, a classical register for the results. The original remains available for ideal unitary statevector inspection. Using a distinct copy avoids accidentally feeding final measurements into a routine intended only for unmeasured evolution.
Mutation and copying are part of program semantics
Most gate-append methods mutate the circuit object by adding instructions. Assigning alias = bell does not copy its instruction list; both names refer to the same object. If measurements are appended through the alias, the original name sees them too. A deliberate copy separates the two descriptions for this workflow.
Similarly, do not assume that an expression returning an instruction handle is itself a new circuit. Follow the documented return type of the method being used. Keeping circuit construction in a named function that returns a complete QuantumCircuit can make later experiments easier to inspect and reproduce.
Names such as bell_preparation are descriptive metadata, not mathematical guarantees. A circuit called Bell can still contain an incorrect control direction or missing Hadamard. Verify behavior from instructions and independent expected states rather than from labels.
Classical bits record specified measurements
A qubit and a classical bit are different objects. A measurement maps a chosen quantum degree of freedom to a particular classical destination according to the circuit's instructions. The number of classical bits need not always equal the number of qubits, and a circuit can contain qubits that are never measured.
In this simple measure-all case, the output register has one bit for each qubit. The ideal Bell experiment yields only strings 00 and 11, each with probability one half. A finite sample would generally have unequal counts. Those exact probabilities do not justify inventing a particular histogram when the SDK snippet has not been run.
More elaborate programs can use several classical registers, nontrivial measurement maps, and conditional operations. Their result formats require careful interpretation. Later lessons isolate those issues; this first example keeps the mapping simple and avoids assuming that every string is always a direct listing of physical qubits.
Keep software evidence honest
A documentation check establishes that an API is described by the cited official reference. It is weaker than executing the exact snippet in a pinned environment. An independent mathematical check confirms the intended circuit behavior, but cannot detect every SDK-specific syntax, packaging, or environment problem.
For a reproducible run, record the actual SDK and Python versions, the source circuit, the execution method, and any outputs. If using a different SDK release, inspect its documentation rather than silently mixing old tutorials with new interfaces. No installation command or package upgrade is required to understand the mathematics in this lesson.
The practical learning goal is to separate circuit intent, object construction, mathematical evaluation, and external execution. That separation makes a program easier to test and keeps a local educational example from accidentally acquiring hardware or account dependencies.
Four exercises with worked solutions
-
What state appears immediately after h(0) on an initial two-qubit zero register?
Solution. Qubit zero is the rightmost bit, so the state is (|00⟩ + |01⟩)/√2, with amplitudes at indices zero and one.
-
Why use bell.copy before adding measurements?
Solution. It preserves an unmeasured preparation circuit for statevector inspection while creating a separate measured description. A simple second variable assignment would share the same mutable object.
-
Does drawing the circuit produce samples?
Solution. No. Drawing renders the instruction structure. Sampling requires a separate evaluation method and a shot count or equivalent sampling configuration.
-
What should be reported about the SDK example's execution status here?
Solution. It is official-documentation-checked but not executed in the authoring environment. The ideal Bell behavior was checked independently using standard-library code, which is a different kind of evidence.
Five explained multiple-choice questions
-
Appending a gate to QuantumCircuit primarily: A. Updates a circuit description. B. Runs that gate immediately on remote hardware. C. Samples its measurement distribution. D. Calibrates a physical pulse automatically.
Answer: A. Circuit construction is separate from any chosen simulation or execution step.
-
In cx(0,1), the intended roles are: A. q1 control and q0 target. B. Both qubits measured in Z. C. Roles selected from the diagram's leftmost output label. D. q0 control and q1 target.
Answer: D. The documented method arguments specify control first and target second.
-
Assigning measured = bell rather than making a copy would: A. Automatically remove all classical bits. B. Give two names referring to the same mutable circuit object. C. Preserve two independent instruction lists. D. Execute the circuit before measurement.
Answer: B. Appending through either name would modify the shared object.
-
The expected Bell counts from a finite run are: A. Exactly equal for every even shot count. B. Uniform over all four strings. C. Supported on 00 and 11 ideally, with random finite-sample imbalance. D. Fixed to equal counts whenever the sampler and compiler share a seed.
Answer: C. The exact probabilities are one half each, while individual sampled counts fluctuate.
-
The evidence supplied for the SDK snippet is: A. A hardware execution log. B. A claim that every future SDK version behaves identically. C. A measured calibration of the reader's installed environment. D. Official API checking plus a separate local mathematical check, with SDK execution explicitly unperformed.
Answer: D. Distinguishing these evidence types avoids overstating validation.
Pitfalls and summary
Separate descriptions from runs, declare ordering, copy mutable circuits deliberately, and preserve an unmeasured preparation for statevector work. Record actual versions and execution status instead of treating documentation checks as experimental results.
Primary sources and further study
These sources support the definitions and context. The numerical examples, exercises, and explanations here were prepared for this lesson. Research papers are optional advanced reading.