Bug: `create_encode_subroutine` omits the Z correction, so `qecl.encode ["zero"]` does not prepare the logical zero state

Open
#3,492 0 comments 0 reactions 1 assignee View on GitHub

Maintainers usually reply within 1 day

@pragati903 is already working on this.

Since Nov 17, 2026.

  • #3535 by @pragati903 — open

Assessment

This issue has not been assessed yet.

Description

bug good first issue

Summary

Component: frontend/catalyst/python_interface/transforms/qecp/convert_qecl_to_qecp.py — ConvertQecLogicalToQecPhysicalPass.create_encode_subroutine

Severity: Low; this QEC pipeline is a proof-of-concept only, and not used in any meaningful application. However, it silently produces wrong results. The pass emits well-formed IR that compiles and runs, but prepares the wrong state.

create_encode_subroutine measures the X stabilizer generators but then discards the measurement outcomes and deallocates the auxiliary qubits. The classically controlled Z correction is missing. See for example step 3 of the procedure in arXiv:0905.2794, §VIII.A.

The subroutine therefore returns a random element of the set ${Z_{\vec e},|0\rangle_L}$ rather than $|0\rangle_L$. For the Steane code, the returned state lies outside the code space seven times out of eight. We didn't catch the issue initially because our noise-injection pass, InjectNoiseToQECLPattern, only inserts random noise before QEC-cycle ops, hence the state-preparation subroutine was always noise-free and the prepared state was always $|0\rangle_L$.

The issues

The docstring of create_encode_subroutine says the correction is deliberately left out because "the encode op is followed directly by a full cycle of error correction when lowering to the qecl dialect." That reasoning does not hold:

  1. The invariant is unenforced and crosses pass boundaries. ExtractOpConversion in convert_quantum_to_qecl.py does currently emit a qecl.qec after each extract_block, but nothing checks it, and UnrollEncodeLoop in convert_qecp_to_quantum.py moves these calls around.
  2. The rescue only works for perfect codes. It relies on the following cycle's decoder being able to resolve every syndrome, which is true for Steane and not true in general. In principle, it should be possible to generalize the QEC compilation pipeline to other k=1 CSS stabilizer codes, so the encoder must stand on its own.
  3. A subroutine named encode_zero_<code> should prepare the zero state. Correctness that depends on an unrelated call being made afterwards is a trap for the next reader.

Steps to reproduce

builtin.module @module_circuit {
  func.func @test_func() attributes {quantum.node} {
    %0 = "test.op"() : () -> !qecl.codeblock<1>
    %1 = qecl.encode ["zero"] %0 : !qecl.codeblock<1>
    return
  }
}

Run ConvertQecLogicalToQecPhysicalPass(qec_code=QecCode.get("Steane")) and inspect @encode_zero_Steane.

Current IR

Steane, x_tanner rows supported on ${0,1,2,3}$, ${1,2,4,5}$ and ${2,3,5,6}$. SSA names are readable stand-ins for the printed numbering.

func.func private @encode_zero_Steane(%cb: !qecp.codeblock<1 x 7>) -> !qecp.codeblock<1 x 7> {
  %a0 = qecp.alloc_aux : !qecp.qubit<aux>
  %a1 = qecp.alloc_aux : !qecp.qubit<aux>
  %a2 = qecp.alloc_aux : !qecp.qubit<aux>

  %a0_h = qecp.hadamard %a0 : !qecp.qubit<aux>
  %a1_h = qecp.hadamard %a1 : !qecp.qubit<aux>
  %a2_h = qecp.hadamard %a2 : !qecp.qubit<aux>

  %q0 = qecp.extract %cb[0] : !qecp.codeblock<1 x 7> -> !qecp.qubit<data>
  %q1 = qecp.extract %cb[1] : !qecp.codeblock<1 x 7> -> !qecp.qubit<data>
  %q2 = qecp.extract %cb[2] : !qecp.codeblock<1 x 7> -> !qecp.qubit<data>
  %q3 = qecp.extract %cb[3] : !qecp.codeblock<1 x 7> -> !qecp.qubit<data>
  %q4 = qecp.extract %cb[4] : !qecp.codeblock<1 x 7> -> !qecp.qubit<data>
  %q5 = qecp.extract %cb[5] : !qecp.codeblock<1 x 7> -> !qecp.qubit<data>
  %q6 = qecp.extract %cb[6] : !qecp.codeblock<1 x 7> -> !qecp.qubit<data>

  // X check 0, support {0, 1, 2, 3}
  %a0_a, %q0_1 = qecp.cnot %a0_h, %q0 : !qecp.qubit<aux>, !qecp.qubit<data>
  %a0_b, %q1_1 = qecp.cnot %a0_a, %q1 : !qecp.qubit<aux>, !qecp.qubit<data>
  %a0_c, %q2_1 = qecp.cnot %a0_b, %q2 : !qecp.qubit<aux>, !qecp.qubit<data>
  %a0_d, %q3_1 = qecp.cnot %a0_c, %q3 : !qecp.qubit<aux>, !qecp.qubit<data>

  // X check 1, support {1, 2, 4, 5}
  %a1_a, %q1_2 = qecp.cnot %a1_h, %q1_1 : !qecp.qubit<aux>, !qecp.qubit<data>
  %a1_b, %q2_2 = qecp.cnot %a1_a, %q2_1 : !qecp.qubit<aux>, !qecp.qubit<data>
  %a1_c, %q4_1 = qecp.cnot %a1_b, %q4   : !qecp.qubit<aux>, !qecp.qubit<data>
  %a1_d, %q5_1 = qecp.cnot %a1_c, %q5   : !qecp.qubit<aux>, !qecp.qubit<data>

  // X check 2, support {2, 3, 5, 6}
  %a2_a, %q2_3 = qecp.cnot %a2_h, %q2_2 : !qecp.qubit<aux>, !qecp.qubit<data>
  %a2_b, %q3_2 = qecp.cnot %a2_a, %q3_1 : !qecp.qubit<aux>, !qecp.qubit<data>
  %a2_c, %q5_2 = qecp.cnot %a2_b, %q5_1 : !qecp.qubit<aux>, !qecp.qubit<data>
  %a2_d, %q6_1 = qecp.cnot %a2_c, %q6   : !qecp.qubit<aux>, !qecp.qubit<data>

  %cb_1 = qecp.insert %cb[0],   %q0_1 : !qecp.codeblock<1 x 7>, !qecp.qubit<data>
  %cb_2 = qecp.insert %cb_1[1], %q1_2 : !qecp.codeblock<1 x 7>, !qecp.qubit<data>
  %cb_3 = qecp.insert %cb_2[2], %q2_3 : !qecp.codeblock<1 x 7>, !qecp.qubit<data>
  %cb_4 = qecp.insert %cb_3[3], %q3_2 : !qecp.codeblock<1 x 7>, !qecp.qubit<data>
  %cb_5 = qecp.insert %cb_4[4], %q4_1 : !qecp.codeblock<1 x 7>, !qecp.qubit<data>
  %cb_6 = qecp.insert %cb_5[5], %q5_2 : !qecp.codeblock<1 x 7>, !qecp.qubit<data>
  %cb_7 = qecp.insert %cb_6[6], %q6_1 : !qecp.codeblock<1 x 7>, !qecp.qubit<data>

  %a0_f = qecp.hadamard %a0_d : !qecp.qubit<aux>
  %a1_f = qecp.hadamard %a1_d : !qecp.qubit<aux>
  %a2_f = qecp.hadamard %a2_d : !qecp.qubit<aux>

  // BUG: %m0, %m1 and %m2 have no uses.
  %m0, %a0_out = qecp.measure %a0_f : i1, !qecp.qubit<aux>
  %m1, %a1_out = qecp.measure %a1_f : i1, !qecp.qubit<aux>
  %m2, %a2_out = qecp.measure %a2_f : i1, !qecp.qubit<aux>

  qecp.dealloc_aux %a0_out : !qecp.qubit<aux>
  qecp.dealloc_aux %a1_out : !qecp.qubit<aux>
  qecp.dealloc_aux %a2_out : !qecp.qubit<aux>

  func.return %cb_7 : !qecp.codeblock<1 x 7>
}

The three dead i1 results are the bug in one line of IR.

Requirements

We only need to support the Steane code in this proof-of-concept pipeline, therefore the protocol described in arXiv:0905.2794, §VIII.A will suffice. That is, apply a Z correction on qubit $i = 1^{m_1} + 2^{m_2} + 4^{m_0}$, where $m_0, m_1, m_2$ are the outcomes of measuring auxiliary qubits 0, 1, and 2 in the encoding step, respectively.

However, if it sufficiently straightforward to implement a solution that applies to all $k=1$ CSS codes, then that solution would be preferred.

Dominant language
Python
Stars
234
Forks
84
Avg merge
3d 11h
Merged PRs (30d)
78

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from PennyLaneAI/catalyst

All issues in PennyLaneAI/catalyst

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.