8 min read
How Circuits and Gates Take Their Arguments
The different shapes qubit and parameter arguments come in, single values, lists, registers, and register indices.
Suggested before this lesson
You've been writing qc.h(0), qc.cx(0, 1) for a few lessons now. Before going further, it's worth pinning down exactly what these methods accept: Qiskit is fairly flexible here, and knowing the options will make circuits you read (and write) elsewhere much less mysterious.
Most single-qubit gate methods accept either a single qubit index, or a list of qubit indices, applying the gate to each one independently, in one call:
That's exactly equivalent to writing qc.h(0), qc.h(1), qc.h(2) on three separate lines: Qiskit just expands the list for you. This is called broadcasting, and it's purely a convenience: the resulting circuit has three independent gates in it either way.
Circuit
h
Step through the circuit: after gate 3 of 3
4 / 4
Measurement Probabilities
12.5%
12.5%
12.5%
12.5%
12.5%
12.5%
12.5%
12.5%
.h(...).Live preview of the circuit detected in your code
Broadcasting works for the plain single-qubit gates (x, y, z, h, s, t, ...). Two-qubit gates like cx don't broadcast the same way: qc.cx(0, 1) always means exactly one control and one target.
When you build a circuit from a QuantumRegister, indexing into it (qr[0]) gives you back a Qubit object, not a plain number. You can pass that object anywhere a gate expects a qubit, and it means exactly the same thing as the plain index:
Why bother, if it means the same thing? Once circuits use multiple registers, qr[0] and ancilla[0] are both "qubit 0 of some register"; the register makes it unambiguous which one you mean. For a circuit with a single register, it's purely a style choice.
Live preview of the circuit detected in your code
rx, ry, rz each take a single numeric angle before the qubit: qc.rx(theta, 0). Some gates need more than one number. The most general single-qubit gate, u, takes three angles:
Every single-qubit gate you've learned so far (, , , and the rest) can be written as some particular choice of u(theta, phi, lambda). You won't need u directly in these lessons, but it's worth recognizing when you see it: it's not a "different kind" of gate, just the fully general form the specific ones are special cases of.
| What | Shapes it accepts |
|---|---|
| A qubit argument | plain int (0), or a register index (qr[0]); both mean the same qubit |
| A "which qubits" argument for a broadcastable gate | a single qubit, or a list of qubits ([0, 1, 2]) |
| A two-qubit gate's qubits | exactly two: (control, target) for cx, or (a, b) for swap |
| A gate's numeric parameters | one angle (rx, ry, rz), or several (u takes three) |
Python
Python
Python
Python
Python