Verifying Stean e Code
Verifying Stean e Code
In this paper we give a partially mechanized proof of the correctness of Steane’s 7-qubit error correcting
code, using the tool Quantomatic. To the best of our knowledge, this represents the largest and most
complicated verification task yet carried out using Quantomatic.
1 Introduction
Even more so than their classical equivalents, quantum information processing technologies are susceptible
to noise and other errors, which can destroy stored data and render computations meaningless. For
this reason, quantum error-correcting codes are seen as a crucial ingredient of any practical quantum
information scheme.
Conceptually, error-correcting codes are simple: the space of possible messages is embedded in a
larger code-space. If an error occurs on the encoded message, it will (hopefully) be mapped to an element
of the code-space that does not correspond to any message. By careful choice of the code-space, and if
the error rate is not too high, it should be possible to recover the original message.
For example, consider the classical repetition code. Here a bit is mapped to a code-space consisting of
three bits:
0 7→ 000 1 7→ 111
If a single bit of the codeword is flipped, then the original encoded bit can be recovered by taking the
majority of the three bits. Notice that this code only protects against the specific error model where only
one bit can be flipped: if two (or more) bits are flipped then the code cannot correct the error and the
encoded bit will be flipped.
The classical case is simpler than the quantum case1 for two reasons: the only possible errors are bit
flips; and, it is possible to measure a classical bit without changing its value. However neither of these
problems is fatal. Although the errors which may afflict a qubit might be any unitary map, it suffices to
coherently correct Pauli X and Z errors, since these generate all the others. To detect the errors in the first
place, quantum codes typically entangle the code-word with an ancilla and measure the ancilla. For the
full details see, for example, [7], and references therein.
While the simple repetition code described above can be adapted for the quantum case, at a cost
of 9 qubits for each encoded qubit, we will consider a 7-qubit code due to Andrew Steane [8]. Like
the repetition code, the Steane code can only correct single qubit errors2 . Practically speaking the code
consists of three circuits: an encoding circuit, a decoding circuit (adjoint to the encoder), and an error
correcting circuit that tests the code-space for errors and corrects them.
1 In
fact, we consider the simplest possible case for both: errors affect only a single bit/qubit. This is not terribly realistic.
2 Thisis not exactly true. As we shall see later, the code can correct a single Z error and/or a single X error, but they do not
have to affect the same qubit.
... ...
α, Φ α, Φ H
... ...
2 The ZX-calculus
The ZX-calculus is a formal language for reasoning about quantum computational systems. It comprises
a graphical syntax and a set of axioms presented as rewrite rules. It has a standard semantics in Hilbert
spaces, and can represent any quantum circuit. We briefly review the ZX-calculus here; for full detalis
consult [1]; here we make use of the ZX-calculus extended with conditional vertices, as elaborated in
[5, 6].
Definition 2.1. Let S be some set of variables. A diagram over S is an open graph whose interior vertices
are restricted to the following types:
• Z-vertices, labelled by an angle α ∈ [0, 2π) and a boolean formula Φ with variables from S; these
are shown as (light) green circles,
• X vertices, labelled by an angle α ∈ [0, 2π) and a boolean formula Φ with variables from S; these
are shown as (dark) red circles,
• H (or Hadamard) vertices, restricted to degree 2; shown as squares.
The allowed vertices are shown in Figure 1.
If an X or Z vertex is labelled by α = 0 then the label is omitted. If the labelling formula Φ is not
constant then the vertex is called conditional; otherwise it is unconditional. We omit the formulae from
unconditional vertices.
For each S, the diagrams over S form a symmetric monoidal category (in fact compact closed) in the
obvious way: the objects of the category are natural numbers and an arrow g : m → n is a diagram with m
input and n outputs. The tensor product is juxtaposition, and composition g ◦ f is defined by identifying
the output vertices of f with the input vertices of g. For more details see [4, 3]. Denote this category
D(S); we denote the category D(0) / of unconditional diagrams by D. Note that the components shown in
Figure 1 are the generators of D(S).
Definition 2.2. Let v : S → {0, 1} be any boolean function; it naturally assigns a truth value v(Φ) to any
formula Φ over S. We define a valuation functor v̂ : D(S) → D by relabelling the Z and X vertices as
follows:
α if v(Φ) = 1
α, Φ 7→
0 otherwise
R. Duncan & M. Lucas 35
Definition 2.3. Let J·K : D → fdHilb be a traced monoidal functor; define its action on objects by
n
JnK = C2 ; define its action on the generators as:
u }
...
|0i⊗m 7→ |0i⊗n
α ~ =
|1i⊗m 7→ eiα |1i⊗n
v
...
u }
...
|+i⊗m 7→ |+i⊗n
α ~ =
|−i⊗m 7→ eiα |−i⊗n
v
...
u }
v H ~ = √1 1 1
.
2 1 −1
Definition 2.4. The denotation of a diagram D over variables S is a superoperator constructed by summing
over all the valuations of S:
ρ 7→ ∑ Jv̂(D)KρJv̂(D)K† .
v∈2S
Example 2.5 (Unitary gates). Any quantum circuit can be written in the ZX-calculus. This is most easily
seen via the translation of a universal gate set:
t |
cos α2 −i sin α2
Xα = = α
−i sin α2 cos α2
t |
1 0
Zβ = = β
0 eiβ
u }
1 1 1
H = √
2 1 −1
= v H ~
1 0 0 0 t |
0 1 0 0
=
0
. =
0 0 1
0 0 1 0
Of course, one easily incorporates state preparations as well. For example:
s { s {
|0i 1 |1i 0 π
= = = =
0 1
We represent
√ measurements by combining a conditional Pauli and a projection. Let |α± i = (|0i ±
eiα |1i)/ 2, then
u }
w
v π, {i}
~ = ρ →
7 hα + | (1ρ1 + ZρZ) |α+ i = hα + | ρ |α+ i + hα− | ρ |α − i
−α
is the measurement in the basis |α± i. Notice that here the variable i represents the outcome of the
measurement; hence to properly encode a measurement it should always be a fresh variable.
Axioms. The ZX-calculus calculus is also an equational theory; its axioms are shown in Figure 2. Two
key properties are:
• Soundness: If D = D0 via the equational rules, then JDK = JD0 K.
• Colour Duality: If D1 = D2 is a derivable equation in the ZX-calculus then D01 = D02 is also derivable,
where D0i is obtained from Di by exchanging the colours red and green.
Example 2.7 (The ∧Z-gate). The ∧Z-gate can be obtained by using a Hadamard gate to transform the
target bit of a ∧X gate.
H H
7→ = H
H
H
The encoder The encoder maps a single input qubit to the 7-qubit code-space via the circuit shown in
Figure 3 (a). Note that qubit 3 is the input. Using the translation described in Example 2.5 we obtain the
ZX-calculus term shown in Figure 3 (b).
The encoding circuit is simple enough that Quantomatic can reduce it to its minimal form without hu-
man intervention. The resulting simplification is shown below; the final equation is merely a rearrangment
of the vertices to show the structure more clearly.
6 6 5
5 5 1
4 4 0
3 = 3 = 6
H 2 2 3
H 1 1 2
H 0 0 4
R. Duncan & M. Lucas 37
... ...
α, Φ ... π, Φ ...
= α +β,Φ = π, Φ ⊕ Φ0
β,Φ ... π, Φ0 ...
... ...
(spider) (π-spider)
α, Φ = α, Φ 0, Φ =
... ...
(anti-loop) (identity)
... ... ... ...
α, 1 = α α, 0 =
... ... ... ...
(true) (false)
π, Φ −α, Φ
α, Φ = ... =
... α, Φ π, Φ π, Φ
... ...
(copying) (π-commute)
... ...
α, Φ α, Φ
= =
β , Φ0 β , Φ0
... ...
(bialgebra) (hopf)
H α, Φ H
= =
α, Φ H H H
... ...
(H-commute) (H-cancel)
Figure 2: Equational rules for the ZX-calculus. We present the rules for the Z subsystem; to obtain the
complete set of rules exchange the colours in the rules shown above.
38 Verifying the Steane code with Quantomatic
|0i 6 6
|0i 5 5
|0i 4 4
3 3
|0i H 2
H 2
|0i H 1
H 1
|0i H 0
H 0
(a) (b)
|0i H H MX
|0i H H MX
|0i H H MX
|0i H H MX
|0i H H MX
|0i H H MX
Z Z Z Z X
Z Z Z X
Z Z Z X
Z Z Z X
Z Z X
Z Z X
Z Z X
Figure 4: The circuit to correct errors. Note that the conditions to activate the Pauli X and Z corrections
are not shown.
The error corrector An encoder is not very useful if we cannot detect and correct errors on the encoded
data. The circuit for carrying out this function is shown in Figure 4. Notice that the circuit naturally splits
into two parts. The error-detecting part introduces a 6-qubit ancilla, entangles it with the input, and then
measures the ancillae. The resulting measurements are called the error syndrome. The error-correcting
part comprises the Pauli operations at the end, which are applied conditionally, depending on the value of
the error syndrome. One crucial detail has been omitted from this picture: we do not show the conditions
which control the error-correcting part. We will derive these conditions in the next section.
We translate the circuit into the ZX-calculus as shown in Figure 5. The variables a, b, . . . , f indicate
the outcomes of the six syndrome measurments, while the variables xi and zi indicate whether a Pauli X or
Z correction need be applied on qubit i of the codeword.
R. Duncan & M. Lucas 39
π, f
H H
π, e
H H
π, d
H H
π, c
H H
π, b
H H
π, a
H H
H
H H
π, z6 π, x6
H H π, z5 π, x5
H H
π, z4 π, x4
H H π, z3 π, x3
H H π, z2 π, x2
H
π, z1 π, x1
π, z0 π, x0
= =
H H
... ...
...
Lemma 3.2.
π, v π, v π, v
H H
= =
... ... ...
By three applications of each lemma, the correcting circuit rewrites to the diagram in Figure 6. Notice
that this is not a minimal form, however it is well adapted to the analysis of the next section.
π, a π, b π, c π, d π, e π, f
π, z6 π, x6
π, z5 π, x5
π, z4 π, x4
π, z3 π, x3
π, z2 π, x2
π, z1 π, x1
π, z0 π, x0
4 Verification
In this section we will demonstrate the correctness of the Steane error-correcting code. There are two
parts to this argument. Firstly we show that the correcting circuit can remove single qubit Pauli errors;
and secondly, we show that the composition of encoder, corrector, and decoder will reduce to the identity
for one qubit.
Consider again the error-correcting circuit in Figure 6. The Pauli Z on qubit i must be activated exactly
when there is a Z-error on qubit i: therefore the variable zi must be true exactly in this case. However the
only available information are the syndrome measurements a, b, . . . , f . Therefore the first step is to derive
the value of the zi and xi in terms of these variables.
R. Duncan & M. Lucas 41
π, a π, b π, c π, d π, e π, f
Note the colour-symmetry of the error-detection sub-circuit (Figure 7). From this we can immediately
see that the first three measurements are resonsible for detecting X errors, while the remaing three are
responsible for Z errors. The variables a, b, c then form the X-syndrome, and d, e, f the Z-syndrome.
Viewing the each sub-syndrome as a bit-string, the eight possible values correspond to the 7 qubits of the
codeword, and the possibility “no error”.
As a typical example, we derive the value of z6 , indicating a Z-error on qubit 6. Ignoring the other
qubits of the codeword we have the following situation:
π, a π, b π, c π, d π, e π, f
π, z6
By repeatedly applying the α-commute, copying, and spider rules, this can be rewritten as follows:
π, f ⊕ z6
π, a π, b π, c π, e ⊕ z6
π, d ⊕ z6
π, z6
Now it is easy to see that the diagram will only be deterministic (with respect to Z errors) on the condition
that
(z6 ↔ d) ∧ (z6 ↔ e) ∧ (z6 ↔ f )
from which we derive z6 = d ∧ e ∧ f . Similar analysis can derive the values for the other variables, shown
in the table below.
42 Verifying the Steane code with Quantomatic
π, a π, b π, c π, d π, e π, f
π, de f π, abc π, de f π, abc
π, de f π, abc π, de f π, abc
π, de f π, abc π, de f π, abc
π, de f π, abc π, de f π, abc
π, de f π, abc π, de f π, abc
π, de f π, abc π, de f π, abc
π, de f π, abc π, de f π, abc
(a)
(b)
(c)
Figure 8: (a) The error-correcting circuit with errors. (b) The reduced form (c) The unconditional form.
NB: we write ab for a ∧ b and a for ¬a.
R. Duncan & M. Lucas 43
Qubit i xi zi
6 a∧b∧c d ∧e∧ f
5 a ∧ b ∧ ¬c d ∧e∧¬f
4 a ∧ ¬b ∧ c d ∧ ¬e ∧ f
3 ¬a ∧ b ∧ c ¬d ∧ e ∧ f
2 ¬a ∧ ¬b ∧ c ¬d ∧ ¬e ∧ f
1 ¬a ∧ b ∧ ¬c ¬d ∧ e ∧ ¬ f
0 a ∧ ¬b ∧ ¬c d ∧ ¬e ∧ ¬ f
Now we can write down the complete circuit, including the errors, as shown in Figure 8 (a). Notice that
the choice of conditional formulae for the errors enforces the desired error model. By propagating the
errors forward, as above, we arrive at the diagram of Figure 8 (b). It is easily verified that the boolean
expressions in the remaining conditional vertices are all uniformly false, hence they can be dropped.
Therefore we conclude that the correcting circuit does indeed correct any of the claimed errors.
The final thing we will check is that the composition of the encoding circuit, the correcting circuit, and
the decoding circuit combine to yield the identity for a single qubit. The combined graph is rather large,
so we will not show it here. Quantomatic was not able to carry out the proof completely automatically,
and several lemmas had to be added as new rules. However, with some human intervention the circuit
does indeed reduce to the desired form. The proof can be found in the appendix.
5 Discussion
We have shown that Steane’s 7-qubit error-correcting code performs as advertised. This is perhaps not
surprising. The main achievement here was to carry out the proof in the automated rewriting system
Quantomatic. Since any practical quantum program will have to be implemented “underneath” an
error-correcting code of some kind we view this as a first step towards mechanically verifying practical
programs.
Since (to our knowledge) this is the largest test yet carried out using Quantomatic, a few comments
are in order. There were two obstacles to this work. Firstly, the ZX-calculus with conditional vertices
is not implemented in Quantomatic, so this part had to be done either by hand or via case analysis. In
particular it was necessary to simplify the boolean expressions. It should be possible to add such features
to the program. The more significant problem was the lack of good strategies for fully automatic work.
Since many of the rewrite rules must be used as expansions rather than reductions, manual intervention is
required. As glance at the appendix will tell the reader that we have already reached the stage where it is
not easy for the human operator to see what he is doing.
The Steane code is not optimal, in the sense that it uses more qubits than the theoretical minimum of
5, which is attained by other codes. The reason we chose to study Steane code is that quantum logic gates
can be implemented directly on the code-space, and these have a particularly simple form. The study of
the encoded operations, and the verification of encoded circuits is a subject for future work.
References
[1] Bob Coecke & Ross Duncan (2011): Interacting Quantum Observables: Categorical Algebra
and Diagrammatics. New J. Phys 13(043016), doi:10.1088/1367-2630/13/4/043016. Available at
http://iopscience.iop.org/1367-2630/13/4/043016/.
44 Verifying the Steane code with Quantomatic
[2] L. Dixon, R. Duncan, B. Frot, A. Kissinger, A. Merry & M. Soloviev: Quantomatic. Available at
https://sites.google.com/site/quantomatic/.
[3] Lucas Dixon & Ross Duncan (2009): Graphical Reasoning in Compact Closed Categories for Quantum Com-
putation. Annals of Mathematics and Artificial Intelligence 56(1), pp. 23–42, doi:10.1007/s10472-009-9141-x.
[4] R Duncan (2006): Types for Quantum Computing. Ph.D. thesis, Oxford University.
[5] R. Duncan & S. Perdrix (2010): Rewriting measurement-based quantum computations with generalised flow. In
S. Abramsky, C. Gavoille, C Kirchner, F. Meyer auf der Heide & P. G. Spirakis, editors: Automata, Languages
and Programming, 37th International Colloquium, ICALP 2010, Proceedings Part II, Lecture Notes in Computer
Science 6199, Springer, pp. 285–296, doi:10.1007/978-3-642-14162-1_24.
[6] Ross Duncan (2013): A graphical approach to measurement-based quantum computing. In Chris Heunen,
Mehrnoosh Sadrzadeh & Edward Grefenstette, editors: Quantum Physics and Linguistics: A Compositional, Di-
agrammatic Discourse, chapter 3, Oxford University Press, doi:10.1093/acprof:oso/9780199646296.003.0003.
[7] N. David Mermin (2007): Quantum Computer Science. Cambridge University Press,
doi:10.1017/CBO9780511813870.
[8] Andrew Steane (1996): Multiple-Particle Interference and Quantum Error Correction. Proc. R. Soc. Lond. A
8(1954), pp. 2551–2577, doi:10.1098/rspa.1996.0136.
R. Duncan & M. Lucas 45
Note that the permutation between the two circuits is not strictly necessary. We rewrite this diagram as
described in Table 1, and illustrated in Figure 9.
(1)
(2)
(3)
(4)
(5)
The rewriting sequence is described in Table 2 and illustrated in Figures 10 and 11. The final diagram is a
single wire, hence the cicruit rewrites to the identity, as required.
(6)
(7)
(8)
(9)
(10)
(11)
(12)
(13)
(14)
(15)
(16)
(17)