Background

How quantum computing works, and what our slicer does with it

Enough quantum mechanics to read the graphs on this site, followed by the details of the analysis itself. No physics background assumed.

Qubits and superposition

A classical bit is 0 or 1. A qubit holds a pair of complex numbers — amplitudes — one for $|0>$ and one for $|1>$, whose squared magnitudes sum to 1. Writing $\alpha|0> + \beta|1>$ says the qubit will read as 0 with probability $|\alpha|^{2}$ and 1 with probability $|\beta|^{2}$.

The important part is that $\alpha$ and $\beta$ can be negative or complex. That is what lets amplitudes cancel. Every quantum algorithm worth writing is an arrangement for making the wrong answers cancel and the right ones add up, which is also why a phase that looks invisible — it changes no probability on its own — can matter enormously three gates later.

Gates

Gates are the instructions. Each one is a reversible rotation applied to one or a few qubits: h spreads a definite state into an even superposition, x flips it, rz turns its phase, cx flips one qubit conditioned on another. Many gates are their own inverse, so applying one twice gets you back where you started — the analyzer uses exactly this to point out likely uncomputation.

q0 q1 H M M
The Bell pair. After h and cx, the two measurements always agree — both 00 or both 11, never one of each — even though neither qubit had a definite value beforehand.

Entanglement

Two qubits are entangled when their joint state cannot be expressed as a product of independent states for the two qubits. In that case, describing each qubit separately does not capture all the correlations in the pair. In the Bell pair above there is no answer to “what is q0 doing?” that does not mention q1. This is the thing that makes quantum slicing hard: a gate on q1 can change how the outcomes of measuring q0 and q1 correlate, even though no instruction names both. Operations on entangled qubits can affect joint measurement statistics and the correlations observed between their outcomes.

Our analyzer approximates this without simulating. It keeps groups: the qubits and measurements whose outcomes may be correlated. Every wire starts in its own group, and a multi-qubit gate merges the groups of the qubits it touches. Beside the groups it follows each qubit's state while that is provably one of a few simple ones (|0>, |1>, |+> and the like), which keeps a gate that cannot entangle from merging anything. The result is an over-approximation — it can claim entanglement that a full simulation would show has canceled out — which is generally the safer direction for a conservative static analysis to err in.

What joins a group

A multi-qubit gate such as cx, cz, ccx, cswap or rzz, unless the known states rule it out: a control known to be |0> switches the gate off. A measurement joins its qubit's group, and an operation conditioned on a measured bit joins that measurement's.

What leaves one

Only a reset or initialize takes a qubit out. Measuring does not: the outcome stays correlated with the rest of the group, which is what a measurement's hover card reports. Nothing is ever uncomputed out of a group.

Measurement

Measurement produces a classical outcome and changes the quantum state according to the measurement basis. For a projective measurement of a qubit in the computational basis, the post-measurement state is consistent with the observed 0 or 1 outcome. Along with reset, it is one of the only irreversible operations, and it is where a quantum program hands results back to ordinary code — including to if statements that decide which later gates run at all.

Measuring always answers a question in some basis. The default is the Z basis: “is this qubit 0 or 1?” Asking about the X axis instead means applying h first; asking about Y means sdg then h.

What slicing means

A slice is the subset of a program containing only the statements relevant to a particular computation at a particular point. It is defined by a slicing criterion (s, V), where s is a statement and V is the set of variables of interest at that statement — for a quantum program, the qubits and classical bits.

SliceCriterionContains
Static backward SS(s, V) V is used at s Every statement that could affect V at s
Static forward SFS(s, V) V is defined at s Every statement that could be affected by V at s

Both reduce to graph reachability on the QDG. A backward slice finds the nodes that can reach s along dependency edges; a forward slice finds the nodes reachable from it. That is literally what the slicer does: a graph walk over incoming or outgoing edges, filtered by the dependency types you selected. Here the criterion is always one instruction, the node you click, and V is everything that instruction acts on.

Two more questions are built from those walks. A chop from one instruction to another (on the slicer page, an operation and the measurement to stop at) keeps the instructions on some dependence path between them: the forward slice from the first intersected with the backward slice from the second, and empty when the first cannot affect the second. Slicing on a qubit is a backward slice from that qubit's last measurement.

The point is not a smaller program but a semantically equivalent one with respect to the criterion. This implementation approximates that goal through a dependency graph, and its guarantees depend on which dependency rules are implemented and enabled. A slice that respected only classical dependencies could look correct and still produce different measurement outcomes — which is why the graph has to carry the quantum-specific edges below.

The dependency types

Classical program analysis has two: control dependence, where whether a statement runs depends on a condition, and data dependence, where a statement reads a value an earlier one wrote. Quantum programs add physical constraints that neither category can express. Ignoring them does not make the analysis imprecise; it makes it wrong.

Qubit data dependence

A qubit's state is described by a pair of complex amplitudes carrying both measurement likelihoods and relative phase, and every gate transforms the whole of it. So a gate's output is a function of the complete state every prior gate on that wire produced.

This is why a gate that looks irrelevant can be essential: z between two Hadamards changes the final probabilities, even though on its own it changes no probability at all.

Implemented and on by default (DataQubitRule).

Measurement dependence

Measurement is not a passive read. It collapses a qubit into a definite classical value, irreversibly and probabilistically, and any later operation conditioned on that outcome depends on the measurement being there.

Slice out the measurement and the conditional branch is severed entirely. Load the teleportation example and keep only these edges to see it isolated.

Implemented and on by default (MeasurementRule).

Entanglement dependence

Once qubits are entangled, an operation on one can change the meaning of a later operation on the other with no classical data flow between them. The dependence holds when the entanglement has not been uncomputed before measurement.

If the entangling operation is undone before the measurement, it and its reversal can both leave the slice. If it is not, the entangling operation and everything that touched those qubits has to stay.

Implemented (EntanglementRule) but not in the default rule set, so the site does not offer it — an over-approximate rule quickly makes every slice the whole program. The slicer does not detect uncomputation either: the gates that create and undo entanglement stay in a slice through qubit data dependence.

Classical dependence

The ordinary kind, and still needed: classical bits hold measurement outcomes and those outcomes control later operations. Reaching definitions on classical bits are in the default rule set, and so is control dependence: every gate inside an if_test or switch block depends on the test that decides whether it runs.

On by default (DataClassicalRule, ControlClassicalRule).

Deliberately out of scope

No-cloning dependence

An unknown quantum state cannot be copied; an attempt to copy entangles instead. cx(0, 1) after x(0) looks like a copy and is not — q1 becomes conditional on q0.

Out of scope: for slicing purposes this is a subset of entanglement dependence and does not need its own edge type.

Temporal dependence

Qubits decohere. A long enough delay between two gates means the second one acts on a qubit that has already lost its quantum information.

Out of scope: coherence time varies by machine, so there is no machine-independent answer to whether a given delay has destroyed the state.

How our slicer works

  1. Parse into an IR

    A parser turns a circuit into a flat list of QuantumInstruction objects, each with a gate name, qubit indices, classical bit indices and parameters. Neither parser runs your file: each reads it and works out the circuit itself, so every instruction keeps the line that produced it (see Two ways in). Loops whose length is known are unrolled, so each iteration's gates are their own nodes. An if_test or switch becomes a test node that reads the condition's bits, followed by the gates of its branches, each of them a node too.

  2. Build the QDG

    Every instruction becomes a node. Then each dependency rule walks the program once, in order, and emits labeled edges. Rules are independent and pluggable, which is what makes the filter chips on the slicer page meaningful: they select which rules' edges the traversal is allowed to follow. The site runs the four rules that are on by default; only those get a chip.

    DependencyEmitted whenStatus
    Data qubit A gate writes a wire that a later gate reads. Classical def-use, applied per qubit: every qubit a gate acts on, controls included, counts as read and written. barrier and delay take no part, and a reset that always runs cuts the wire. on by default
    Measurement A test node, a bit copy or a conditioned gate reads a bit that a measurement may have written last. on by default
    Data classical Reaching definition on a classical bit: the same, from any instruction that writes the bit. on by default
    Control classical The test node of an if_test or switch to each instruction directly inside its branches. on by default
    Entanglement An entangling gate to later operations on the qubits it correlated. implemented, not in the default rule set
    Phase A phase-changing gate to later operations that could observe the phase. implemented, not in the default rule set

    Inside an if/else, each branch starts from what reached the block. After it, the last writes of every branch reach the next reader, and so does what came before the block unless every possible branch overwrote it (an if without an else never does).

  3. Traverse from a criterion

    A backward slice is a walk along incoming edges from the criterion node, keeping only edges whose dependency type you selected. A forward slice is the same walk along outgoing edges. The criterion node is always included.

  4. Approximate entanglement alongside

    Tracking entanglement exactly would mean carrying an internal representation of the program's quantum state, which is the thing static analysis exists to avoid. The analyzer approximates instead, in one more pass over the program that keeps the groups and known states described under Entanglement. It adds no edges to the QDG, so it never changes a slice. What it finds shows up on the graph:

    • Correlations. A measurement's hover card lists the other measurements it may be correlated with and the unmeasured qubits it may still be entangled with. While you slice from a measurement, those wires are marked too.
    • Kickback, no effect, known outcomes. A controlled gate whose target is an eigenstate of the gate it applies is marked as phase kickback; a gate that provably does nothing says why; a measurement whose outcome is known before running says which.
    • Mirrors. A gate and a later gate that is its inverse on the same qubits (a self-inverse gate repeated, s then sdg, a rotation then its negative) can be linked with the Mirrors toggle. The lint check QD08 reports the simplest case, a self-inverse gate repeated with nothing between them on its wires.

Walkthrough: teleportation

Load Quantum teleportation on the slicer page. The circuit prepares an unknown state on q0, entangles q1 with q2, measures q0 and q1, and then applies corrections to q2 based on those two classical bits. Each correction is two nodes: the if_else test that reads a bit (#8 and #10) and the gate inside it (#9 and #11).

  1. Slice backward from the final measurement (#12). You get all 13 instructions, and here that is the right answer: the measurement of q2 depends on Bob's corrections, the corrections depend on the tests that read the bits Alice's measurements wrote, and those measurements depend on everything done to q0 and q1. Teleportation really does need the whole circuit.
  2. Now turn off every dependency except Measurement. Slice backward from #12 again and the slice collapses to {12} alone. The final measurement is not classically conditioned on anything, so no measurement edge reaches it.
  3. Slice backward from the first test (#8) with only Measurement on. Now you get {7, 8}: the if_else test and the measurement of q1, which wrote the c1 bit it reads. That is what the measurement rule exists to produce, and no data-qubit edge would ever have found it — the correction is on q2, the measurement is on q1. The second test (#10) reads c0 instead, and slicing from it gives {6, 10}. The corrections themselves hang off their tests by Control classical edges.
  4. Chop from the state preparation (#0) to the x correction (#9). You get {0, 1, 4, 7, 8, 9}: the unknown state reaches Bob's first correction only through Alice's cx, the measurement of q1 and the test that reads its bit.
  5. Hover the final measurement (#12). Its card says it may be correlated with the measurements of q0 and q1: the two cx gates put all three qubits in one group, and the corrections conditioned on Alice's bits tie her measurements in as well.
  6. Slice forward from the Bell-pair cx (#3). Everything from there to the end lights up — ten of the thirteen instructions. In a teleportation circuit, that is most of the program.

Two ways in

The analysis is the same either way — both front ends produce the same instruction list, and everything downstream works on that. Which one runs follows the language, and is an explicit choice: the Interpreter dropdown on the slicer page. Loading an example or uploading a file sets it for you; nothing else moves it. Neither one executes anything you submit.

Qiskit (.py)

A small interpreter of our own follows the Python and the Qiskit calls that build the circuit, without running the file, and links every instruction back to its source line. It knows enough Python (loops, functions, comprehensions, seeded random numbers, main() and argparse defaults) and enough of Qiskit to rebuild most programs: registers or loose bits (Qubit(), Clbit(), add_bits), the standard gates and library circuits, compose, inverse and with qc.if_test(...) blocks.

Anything only a real run could decide — a qubit index read from a simulator's result, say — stops it with the line number, rather than a guess.

OpenQASM 2 and 3 (.qasm)

Parsed against the OpenQASM grammar by the language's reference parser, then interpreted without Qiskit. Values known at compile time are worked out. An if or switch on a measured value becomes a test node that reads the bits it depends on, followed by the gates of its branches; a while becomes one node. Copying a measured bit into another keeps the dependency through a store node.

Export one from any Qiskit circuit with qiskit.qasm3.dumps(qc). The quick checks and LintQ are skipped, since they read a Python syntax tree; the dependence checks still run.

Tools we use

Qiskit
IBM's quantum SDK. The framework used to implement most of our sample circuits, and the library whose QuantumCircuit API the Python interpreter models. How Qiskit builds and decomposes library circuits is also why parsed gate names sometimes differ from the source.
OpenQASM
A textual assembly language for circuits. The OpenQASM project's reference parser (the openqasm3 package) reads the grammar, and our own interpreter turns its syntax tree into the instruction list.
NetworkX
Graph library. The QDG is a MultiDiGraph — multi, because two instructions can be joined by several dependencies of different kinds at once.
Matplotlib
Draws the desktop viewer, qslicer viz (qslicer/viz/): the same graph as this site, with slicing, lint markers and the circuit picker, in a window.
Flask
Serves this site and exposes the analysis over a small JSON API.
Static checks
Linters that read your file without running it, alongside the parser. The quick checks (QS000–QS017) report what Python or Qiskit would raise, from the syntax tree and the circuit the parser rebuilds. The dependence checks (QD01–QD03, QD05–QD12) report quantum-side mistakes in the rebuilt circuit and its QDG, such as gates no measured bit depends on, and are the only ones that also read OpenQASM. Those 29 rules are on by default. LintQ (Paltenghi and Pradel, FSE 2024, LQ01–LQ10, off by default) comes as a port that matches the original's warnings and, where CodeQL is installed, as the original queries. Pick them under Lint on the slicer page; the Lint page explains each one.

Gate reference

Reference for the gates documented in the slicer page's hover cards. Entangling gates can create entanglement between the qubits they act on; phase gates are diagonal in the computational basis, so they change only relative phases; self-inverse gates undo themselves when applied twice. Multi-qubit matrices use Qiskit's qubit ordering: the first qubit argument is the least significant bit, so for cx(0, 1) the rows run |q_{1}q_{0}> = |00>, |01>, |10>, |11>.

GateNameWhat it doesMatrixFlags
h Hadamard Puts a basis state into an equal superposition. \frac{1}{\sqrt{2}}\matrix{1 & 1 \\ 1 & -1}
self-inverse
id Identity Does nothing. \matrix{1 & 0 \\ 0 & 1}
p Phase(lambda) Adds a relative phase $\lambda$ to $|1>$. \matrix{1 & 0 \\ 0 & e^{\i\lambda}}
phase
rx RX(theta) Rotation of $\theta$ about the X axis. \matrix{\cos\frac{\theta}{2} & -\i\sin\frac{\theta}{2} \\ -\i\sin\frac{\theta}{2} & \cos\frac{\theta}{2}}
ry RY(theta) Rotation of $\theta$ about the Y axis. \matrix{\cos\frac{\theta}{2} & -\sin\frac{\theta}{2} \\ \sin\frac{\theta}{2} & \cos\frac{\theta}{2}}
rz RZ(theta) Rotation of $\theta$ about the Z axis. \matrix{e^{-\i\theta/2} & 0 \\ 0 & e^{\i\theta/2}}
phase
s S (phase) Quarter turn about Z. \matrix{1 & 0 \\ 0 & \i}
phase
sdg S-dagger Inverse of S, written $S^{\dagger}$. \matrix{1 & 0 \\ 0 & -\i}
phase
sx Square root of X Half a bit flip. \frac{1}{2}\matrix{1+\i & 1-\i \\ 1-\i & 1+\i}
t T (pi/8) Eighth turn about Z. \matrix{1 & 0 \\ 0 & e^{\i\pi/4}}
phase
tdg T-dagger Inverse of T. \matrix{1 & 0 \\ 0 & e^{-\i\pi/4}}
phase
u U(theta, phi, lambda) General single-qubit rotation. \matrix{\cos\frac{\theta}{2} & -e^{\i\lambda}\sin\frac{\theta}{2} \\ e^{\i\phi}\sin\frac{\theta}{2} & e^{\i(\phi+\lambda)}\cos\frac{\theta}{2}}
u1 U1(lambda) Legacy phase gate from older transpiler output. \matrix{1 & 0 \\ 0 & e^{\i\lambda}}
phase
u2 U2(phi, lambda) Legacy gate from older transpiler output. \frac{1}{\sqrt{2}}\matrix{1 & -e^{\i\lambda} \\ e^{\i\phi} & e^{\i(\phi+\lambda)}}
u3 U3(theta, phi, lambda) Legacy general rotation. \matrix{\cos\frac{\theta}{2} & -e^{\i\lambda}\sin\frac{\theta}{2} \\ e^{\i\phi}\sin\frac{\theta}{2} & e^{\i(\phi+\lambda)}\cos\frac{\theta}{2}}
x Pauli-X (NOT) Flips $|0>$ and $|1>$. \matrix{0 & 1 \\ 1 & 0}
self-inverse
y Pauli-Y Bit flip plus phase flip. \matrix{0 & -\i \\ \i & 0}
self-inverse
z Pauli-Z Flips the phase of $|1>$. \matrix{1 & 0 \\ 0 & -1}
phase self-inverse
c3x 3-controlled X X with three controls. \text{permutation}
entangling self-inverse
c4x 4-controlled X X with four controls. \text{permutation}
entangling self-inverse
ccx Toffoli (CCX) Flips the target when both controls are $|1>$. \text{8}\times\text{8 permutation}
entangling self-inverse
ch Controlled-H Hadamard on the target when the control is $|1>$. \matrix{1 & 0 & 0 & 0 \\ 0 & \frac{1}{\sqrt{2}} & 0 & \frac{1}{\sqrt{2}} \\ 0 & 0 & 1 & 0 \\ 0 & \frac{1}{\sqrt{2}} & 0 & -\frac{1}{\sqrt{2}}}
entangling self-inverse
cp Controlled-phase Adds $e^{\i\lambda}$ to the $|11>$ amplitude only. \matrix{1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & e^{\i\lambda}}
entangling phase
crx Controlled-RX Conditional rotation of $\theta$ about X. I \otimes |0><0| + RX(\theta) \otimes |1><1|
entangling
cry Controlled-RY Conditional rotation of $\theta$ about Y. I \otimes |0><0| + RY(\theta) \otimes |1><1|
entangling
crz Controlled-RZ Conditional rotation of $\theta$ about Z. \text{diag}(1, e^{-\i\theta/2}, 1, e^{\i\theta/2})
entangling phase
cswap Fredkin (CSWAP) Swaps two qubits when the control is $|1>$. \text{8}\times\text{8 permutation}
entangling self-inverse
csx Controlled-sqrt(X) Conditional half bit flip. I \otimes |0><0| + \sqrt{X} \otimes |1><1|
entangling
cx CNOT (controlled-X) Flips the target when the control is $|1>$. \matrix{1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \\ 0 & 1 & 0 & 0}
entangling self-inverse
cy Controlled-Y Applies Y to the target when the control is $|1>$. \matrix{1 & 0 & 0 & 0 \\ 0 & 0 & 0 & -\i \\ 0 & 0 & 1 & 0 \\ 0 & \i & 0 & 0}
entangling self-inverse
cz Controlled-Z Flips the phase of $|11>$. \matrix{1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & -1}
entangling phase self-inverse
dcx Double CNOT Two back-to-back CNOTs in opposite directions. \text{permutation}
entangling
ecr Echoed cross-resonance Hardware-native entangling gate. \frac{1}{\sqrt{2}}(IX - XY)
entangling self-inverse
iswap iSWAP Swap with a phase of $\i$ on the swapped amplitudes. \matrix{1 & 0 & 0 & 0 \\ 0 & 0 & \i & 0 \\ 0 & \i & 0 & 0 \\ 0 & 0 & 0 & 1}
entangling
mcphase Multi-controlled phase Adds $e^{\i\lambda}$ to the amplitude where every qubit is $|1>$. \text{diag}(1, 1, 1, 1, 1, 1, 1, e^{\i\lambda})
entangling phase
mcx Multi-controlled X X with an arbitrary number of controls. \text{permutation}
entangling self-inverse
rxx RXX(theta) XX interaction of angle $\theta$. e^{-\i\theta(X \otimes X)/2}
entangling
ryy RYY(theta) YY interaction of angle $\theta$. e^{-\i\theta(Y \otimes Y)/2}
entangling
rzz RZZ(theta) ZZ interaction of angle $\theta$. e^{-\i\theta(Z \otimes Z)/2}
entangling phase
swap SWAP Exchanges two qubits: $|ab> \mapsto |ba>$. \matrix{1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1}
self-inverse
measure Measurement Collapses a qubit and writes the outcome to a classical bit. \text{not unitary}
measure_all Measure all Measures every qubit into a fresh classical register. \text{not unitary}
reset Reset Forces a qubit back to $|0>$. \text{not unitary}
c_if Classical conditional (legacy) Pre-2.0 form of a classically conditioned gate. \text{not unitary}
for_loop Loop with a run-time exit A counted loop that a measured value can end early. \text{not unitary}
if_else Classical conditional Runs a block of gates only when a classical bit matches. \text{not unitary}
store Classical copy Copies a classical value into bits. \text{not unitary}
switch_case Classical switch Runs one of several blocks, chosen by a classical value. \text{not unitary}
while_loop Run-time loop Repeats a block for as long as a classical condition holds. \text{not unitary}
barrier Barrier Optimization fence and visual divider. \text{none}
init Wire initialization Synthetic node for the $|0>$ state of a wire. \text{none}