OQCL   HomeStart Here7 SessionsTextbookProjectsQUEST LabSubmitUse CasesInstructor GuideReferences

Quantum in 7 Sessions › Session 4

1234567

Session 4 — Real hardware: compilation and noise

Session 4 of 7Self-studySimulator only

By the end of this session you can tell a logical circuit from the compiled circuit a device runs, explain why limited connectivity adds gates, and name the main kinds of error.

On this page: Read · Key ideas · Worked example · Try it · Second experiment · Common mistakes · Check yourself · Project

1. Read

Read the chapters first. The summary below is a reminder, not a replacement.

2. Key ideas

3. Worked example

Work through this with pen and paper before touching the code.

Estimate how much routing costs. Suppose every two-qubit gate succeeds with probability 0.99, and ignore every other error.

  1. Logical circuit, 3 two-qubit gates: 0.99³ ≈ 0.970.
  2. Compiled circuit, 6 two-qubit gates: 0.99⁶ ≈ 0.941.
  3. Routing alone moved the estimate from about 97% to about 94%, before counting single-qubit gates, readout, or idle time.

This is a rough model, not a prediction for any particular device. Its use is to compare two versions of the same circuit before you spend hardware time.

4. Try it

Take a 4-qubit GHZ circuit in which one qubit controls all the others, and compile it for a device whose qubits sit in a line. Write down what you expect before you run the code.

from qiskit import QuantumCircuit, transpile

# Logical circuit: a 4-qubit GHZ state with qubit 0 controlling all the others
qc = QuantumCircuit(4)
qc.h(0)
qc.cx(0, 1)
qc.cx(0, 2)
qc.cx(0, 3)

# A device whose qubits sit in a line: 0-1-2-3
line = [[0, 1], [1, 0], [1, 2], [2, 1], [2, 3], [3, 2]]
hw = transpile(qc, basis_gates=["rz", "sx", "x", "cx"], coupling_map=line,
               optimization_level=1, seed_transpiler=7)

print("logical : depth", qc.depth(), " two-qubit gates", qc.count_ops().get("cx", 0))
print("hardware: depth", hw.depth(), " two-qubit gates", hw.count_ops().get("cx", 0))

Output (counts will vary slightly from run to run):

logical : depth 4  two-qubit gates 3
hardware: depth 9  two-qubit gates 6

What to notice

The logical circuit needs 3 two-qubit gates. On a line, qubit 0 is not next to qubits 2 and 3, so the compiler adds routing and the count roughly doubles, with a much larger depth. Nothing about the algorithm changed; only the hardware layout did. Exact numbers can differ between Qiskit versions.

5. Second experiment

Build the same GHZ state as a chain, so that every CNOT acts on neighbouring qubits. Predict first.

from qiskit import QuantumCircuit, transpile

# The same GHZ state, built as a chain: each CNOT acts on neighbours
qc = QuantumCircuit(4)
qc.h(0)
qc.cx(0, 1)
qc.cx(1, 2)
qc.cx(2, 3)

line = [[0, 1], [1, 0], [1, 2], [2, 1], [2, 3], [3, 2]]
hw = transpile(qc, basis_gates=["rz", "sx", "x", "cx"], coupling_map=line,
               optimization_level=1, seed_transpiler=7)
print("hardware: depth", hw.depth(), " two-qubit gates", hw.count_ops().get("cx", 0))

Output:

hardware: depth 6  two-qubit gates 3

What it shows

Now the compiled circuit needs only 3 two-qubit gates, the same as the logical one. The state prepared is identical; only the order of the CNOTs changed. Designing with the hardware layout in mind removed all the routing overhead.

6. Common mistakes and tips

Common mistakes

Tips

7. Check yourself

Answer each question on paper, then open the answer to compare.

  1. Rewrite the logical circuit as a chain (0→1, 1→2, 2→3). It prepares the same GHZ state. What happens to the compiled gate count?
    Show answer

    It stays at 3, because every CNOT already acts on neighbours. See the second experiment.

  2. Why can a quantum error-correcting code not simply copy the qubit three times?
    Show answer

    An unknown quantum state cannot be copied (the no-cloning principle). Error correction instead spreads the information over several qubits using entanglement.

  3. Before spending hardware time, which three numbers would you record for a circuit?
    Show answer

    Circuit depth, number of two-qubit gates, and the number of jobs and shots (with the estimated cost or credits).

8. Do a project

Pick one. Each follows the routine from Guide 1: predict, run the baseline, change one variable, analyze.

← Session 3Session 5: Algorithms: search and phase estimation →

For instructors: Teacher notes for Session 4