Liking cljdoc? Tell your friends :D

org.soulspace.qclojure.domain.error-correction

Quantum error correction codes based on the stabilizer formalism.

This namespace provides a flexible framework for implementing quantum error correction (QEC) codes using stabilizers. The stabilizer formalism is a powerful framework for describing and analyzing quantum error correction codes.

Core Concepts:

  • Stabilizer: A Pauli operator that leaves the code space invariant
  • Stabilizer Generator: A minimal set of stabilizers that generate the full stabilizer group
  • Syndrome: Measurement outcomes that indicate which error occurred
  • Logical Operators: Pauli operators that act on the logical qubit

Supported Codes:

  • Bit-flip code (3-qubit repetition)
  • Phase-flip code (3-qubit in Hadamard basis)
  • Shor code (9-qubit)
  • Steane code (7-qubit)
  • Five-qubit code (5-qubit perfect code)

References:

  • Gottesman, D. (1997). Stabilizer Codes and Quantum Error Correction
  • Nielsen & Chuang, Quantum Computation and Quantum Information, Chapter 10
Quantum error correction codes based on the stabilizer formalism.

This namespace provides a flexible framework for implementing quantum error
correction (QEC) codes using stabilizers. The stabilizer formalism is a
powerful framework for describing and analyzing quantum error correction codes.

Core Concepts:
- Stabilizer: A Pauli operator that leaves the code space invariant
- Stabilizer Generator: A minimal set of stabilizers that generate the full stabilizer group
- Syndrome: Measurement outcomes that indicate which error occurred
- Logical Operators: Pauli operators that act on the logical qubit

Supported Codes:
- Bit-flip code (3-qubit repetition)
- Phase-flip code (3-qubit in Hadamard basis)
- Shor code (9-qubit)
- Steane code (7-qubit)
- Five-qubit code (5-qubit perfect code)

References:
- Gottesman, D. (1997). Stabilizer Codes and Quantum Error Correction
- Nielsen & Chuang, Quantum Computation and Quantum Information, Chapter 10
raw docstring

analyze-error-correction-resultsclj

(analyze-error-correction-results results ctx)

Comprehensive analysis of error correction results from circuit execution. Uses reverse mappings to present all results in terms of ORIGINAL circuit qubits.

This is the main entry point for Phase 2 post-processing. It combines syndrome extraction, decoding, and classical correction to provide a complete analysis of error-corrected circuit results.

Parameters:

  • results: Raw results from circuit execution {:measurements {qubit-idx value, ...}, ...}
  • ctx: Complete optimization context including reverse mappings {:error-correction-code code-definition :logical-to-physical {compacted-logical [physical...], ...} :logical-to-ancillas {compacted-logical [ancilla...], ...} :syndrome-table {...} :inverse-qubit-mapping {compacted-logical original-logical, ...} :qubit-mapping {original-logical compacted-logical, ...} :options {:error-correction-code :bit-flip/:shor, ...}}

Returns: Comprehensive analysis map (all logical qubit keys are ORIGINAL indices): {:raw-results results :syndrome-bits {original-logical [bits...], ...} :decoded-syndromes {original-logical {...}, ...} :error-detected? boolean :corrected-measurements {...} :corrections-applied [{:logical-qubit original-q, ...}, ...] :error-rate float (fraction of logical qubits with errors)}

Example usage: (def optimized-ctx (hw/optimize circuit device {...})) (def results (backend/execute-circuit (:circuit optimized-ctx))) (def analysis (analyze-error-correction-results results optimized-ctx))

;; All results in terms of original circuit (:syndrome-bits analysis) ; {0 [1 0], 2 [0 1]} (original qubits 0 and 2) (:corrected-measurements analysis) ; Get corrected results (:error-rate analysis) ; Check error rate

Comprehensive analysis of error correction results from circuit execution.
Uses reverse mappings to present all results in terms of ORIGINAL circuit qubits.

This is the main entry point for Phase 2 post-processing. It combines
syndrome extraction, decoding, and classical correction to provide a
complete analysis of error-corrected circuit results.

Parameters:
- results: Raw results from circuit execution
  {:measurements {qubit-idx value, ...}, ...}
- ctx: Complete optimization context including reverse mappings
  {:error-correction-code code-definition
   :logical-to-physical {compacted-logical [physical...], ...}
   :logical-to-ancillas {compacted-logical [ancilla...], ...}
   :syndrome-table {...}
   :inverse-qubit-mapping {compacted-logical original-logical, ...}
   :qubit-mapping {original-logical compacted-logical, ...}
   :options {:error-correction-code :bit-flip/:shor, ...}}

Returns:
Comprehensive analysis map (all logical qubit keys are ORIGINAL indices):
{:raw-results results
 :syndrome-bits {original-logical [bits...], ...}
 :decoded-syndromes {original-logical {...}, ...}
 :error-detected? boolean
 :corrected-measurements {...}
 :corrections-applied [{:logical-qubit original-q, ...}, ...]
 :error-rate float (fraction of logical qubits with errors)}

Example usage:
(def optimized-ctx (hw/optimize circuit device {...}))
(def results (backend/execute-circuit (:circuit optimized-ctx)))
(def analysis (analyze-error-correction-results results optimized-ctx))

;; All results in terms of original circuit
(:syndrome-bits analysis)        ; {0 [1 0], 2 [0 1]} (original qubits 0 and 2)
(:corrected-measurements analysis)  ; Get corrected results
(:error-rate analysis)              ; Check error rate
sourceraw docstring

apply-classical-correctionclj

(apply-classical-correction measurements syndrome-info ctx code-key)

Apply classical post-processing correction to measurement results. Works with original logical qubit indices (already mapped by extract-syndrome-bits).

This function performs software correction of measurement outcomes based on syndrome information. It does NOT modify the quantum circuit, but corrects the classical measurement data after execution.

Use cases:

  • Correcting final measurement results from simulators
  • Post-processing experimental data
  • Analyzing error patterns without dynamic circuits

Parameters:

  • measurements: Map of qubit indices to measurement outcomes {0 1, 1 0, 2 1, ...}
  • syndrome-info: Map from ORIGINAL logical qubits to syndrome decode results
  • ctx: Optimization context with mappings {:logical-to-physical {compacted-logical [physical...], ...} :inverse-qubit-mapping {compacted-logical original-logical, ...} :qubit-mapping {original-logical compacted-logical, ...}}
  • code-key: Error correction code keyword

Returns: Map with corrected measurements: {:original-measurements {...} :corrected-measurements {...} :corrections-applied [{:logical-qubit original-q, :physical-qubit p, :correction :x}, ...] :syndrome-info {...}}

Example: Original circuit qubit 2, compacted to 1, physical qubits [3 4 5]

  • Syndrome for original qubit 2: [1 0] → error on first physical qubit (3)
  • Correction flips qubit 3
Apply classical post-processing correction to measurement results.
Works with original logical qubit indices (already mapped by extract-syndrome-bits).

This function performs software correction of measurement outcomes based
on syndrome information. It does NOT modify the quantum circuit, but
corrects the classical measurement data after execution.

Use cases:
- Correcting final measurement results from simulators
- Post-processing experimental data
- Analyzing error patterns without dynamic circuits

Parameters:
- measurements: Map of qubit indices to measurement outcomes {0 1, 1 0, 2 1, ...}
- syndrome-info: Map from ORIGINAL logical qubits to syndrome decode results
- ctx: Optimization context with mappings
  {:logical-to-physical {compacted-logical [physical...], ...}
   :inverse-qubit-mapping {compacted-logical original-logical, ...}
   :qubit-mapping {original-logical compacted-logical, ...}}
- code-key: Error correction code keyword

Returns:
Map with corrected measurements:
{:original-measurements {...}
 :corrected-measurements {...}
 :corrections-applied [{:logical-qubit original-q, :physical-qubit p, :correction :x}, ...]
 :syndrome-info {...}}

Example:
Original circuit qubit 2, compacted to 1, physical qubits [3 4 5]
- Syndrome for original qubit 2: [1 0] → error on first physical qubit (3)
- Correction flips qubit 3
sourceraw docstring

apply-error-correctionclj

(apply-error-correction ctx)

Apply error correction encoding to a circuit context.

This function integrates error correction into the optimization pipeline, transforming the circuit to use physical qubits with error correction.

Process:

  1. Encode ALL logical qubits in the circuit
  2. Create new circuit with enough physical qubits for encoding + ancillas
  3. Apply encoding gates for each logical qubit
  4. Translate all existing operations to work on physical qubits
  5. Add syndrome measurement operations (NEW in Phase 1)

Parameters:

  • ctx: Optimization context containing: :circuit - The quantum circuit (with logical qubits) :options - Map with: :error-correction-code - Keyword for the code to use (e.g., :bit-flip) :apply-error-correction? - Whether to apply error correction (default: false) :include-syndrome-measurement? - Whether to add syndrome measurements (default: true)

Returns: Updated context with:

  • :circuit - Expanded circuit with error correction encoding + syndrome measurement
  • :error-correction-code - The code definition
  • :logical-to-physical - Mapping from logical qubit indices to physical qubit vectors
  • :syndrome-table - Syndrome lookup table
  • :error-correction-applied? - true
  • :syndrome-measurement-included? - true if syndrome measurements were added
Apply error correction encoding to a circuit context.

This function integrates error correction into the optimization pipeline,
transforming the circuit to use physical qubits with error correction.

Process:
1. Encode ALL logical qubits in the circuit
2. Create new circuit with enough physical qubits for encoding + ancillas
3. Apply encoding gates for each logical qubit
4. Translate all existing operations to work on physical qubits
5. Add syndrome measurement operations (NEW in Phase 1)

Parameters:
- ctx: Optimization context containing:
    :circuit - The quantum circuit (with logical qubits)
    :options - Map with:
      :error-correction-code - Keyword for the code to use (e.g., :bit-flip)
      :apply-error-correction? - Whether to apply error correction (default: false)
      :include-syndrome-measurement? - Whether to add syndrome measurements (default: true)

Returns:
Updated context with:
- :circuit - Expanded circuit with error correction encoding + syndrome measurement
- :error-correction-code - The code definition
- :logical-to-physical - Mapping from logical qubit indices to physical qubit vectors
- :syndrome-table - Syndrome lookup table
- :error-correction-applied? - true
- :syndrome-measurement-included? - true if syndrome measurements were added
sourceraw docstring

available-codesclj

Registry of available quantum error correction codes.

Registry of available quantum error correction codes.
sourceraw docstring

bit-flip-codeclj

The 3-qubit bit-flip code protects against single bit-flip (X) errors.

Encoding:

  • |0⟩ → |000⟩
  • |1⟩ → |111⟩

Stabilizer generators: Z₀Z₁, Z₁Z₂

This code can detect and correct a single bit-flip error on any qubit.

The 3-qubit bit-flip code protects against single bit-flip (X) errors.

Encoding:
- |0⟩ → |000⟩
- |1⟩ → |111⟩

Stabilizer generators: Z₀Z₁, Z₁Z₂

This code can detect and correct a single bit-flip error on any qubit.
sourceraw docstring

build-syndrome-tableclj

(build-syndrome-table code)

Build a lookup table mapping syndromes to correctable errors.

For a given stabilizer code, this generates all correctable single-qubit errors and their corresponding syndromes.

Parameters:

  • code: Stabilizer code definition

Returns: Map from syndrome vector to Pauli error string

Build a lookup table mapping syndromes to correctable errors.

For a given stabilizer code, this generates all correctable single-qubit
errors and their corresponding syndromes.

Parameters:
- code: Stabilizer code definition

Returns:
Map from syndrome vector to Pauli error string
sourceraw docstring

correct-bit-flip-errorclj

(correct-bit-flip-error circuit encoded-qubits syndrome)

Apply correction based on bit-flip syndrome.

Syndrome interpretation:

  • [0 0] -> No error
  • [1 0] -> Error on first qubit
  • [1 1] -> Error on second qubit
  • [0 1] -> Error on third qubit

Parameters:

  • circuit: Quantum circuit
  • encoded-qubits: Vector of 3 qubit indices containing the encoded logical qubit
  • syndrome: Vector of 2 measurement outcomes [s0 s1]

Returns: Updated circuit with correction applied

Apply correction based on bit-flip syndrome.

Syndrome interpretation:
- [0 0] -> No error
- [1 0] -> Error on first qubit
- [1 1] -> Error on second qubit
- [0 1] -> Error on third qubit

Parameters:
- circuit: Quantum circuit
- encoded-qubits: Vector of 3 qubit indices containing the encoded logical qubit
- syndrome: Vector of 2 measurement outcomes [s0 s1]

Returns:
Updated circuit with correction applied
sourceraw docstring

correct-five-qubit-errorclj

(correct-five-qubit-error circuit
                          encoded-qubits
                          syndrome
                          &
                          {:keys [syndrome-table]})

Apply correction based on 5-qubit code syndrome.

The syndrome is a 4-bit vector that identifies which qubit (if any) has an error and what type of error occurred (X, Y, or Z).

The 5-qubit code has a syndrome lookup table mapping 4-bit syndromes to specific error patterns. This function uses that table to determine and apply the appropriate correction.

Parameters:

  • circuit: Quantum circuit
  • encoded-qubits: Vector of 5 qubit indices containing the encoded logical qubit
  • syndrome: Vector of 4 measurement outcomes
  • syndrome-table: Optional lookup table mapping syndromes to corrections

Returns: Updated circuit with correction applied

Apply correction based on 5-qubit code syndrome.

The syndrome is a 4-bit vector that identifies which qubit (if any) has an error
and what type of error occurred (X, Y, or Z).

The 5-qubit code has a syndrome lookup table mapping 4-bit syndromes to
specific error patterns. This function uses that table to determine and
apply the appropriate correction.

Parameters:
- circuit: Quantum circuit
- encoded-qubits: Vector of 5 qubit indices containing the encoded logical qubit
- syndrome: Vector of 4 measurement outcomes
- syndrome-table: Optional lookup table mapping syndromes to corrections

Returns:
Updated circuit with correction applied
sourceraw docstring

correct-shor-errorclj

(correct-shor-error circuit
                    encoded-qubits
                    bit-flip-syndrome
                    phase-flip-syndrome)

Apply correction based on Shor code syndrome.

The syndrome has two parts:

  • Bit-flip syndromes: 6 measurements (2 per block) indicating X errors
  • Phase-flip syndromes: 2 measurements indicating Z errors between blocks

Parameters:

  • circuit: Quantum circuit
  • encoded-qubits: Vector of 9 qubit indices
  • bit-flip-syndrome: Vector of 6 measurement outcomes for bit-flip errors
  • phase-flip-syndrome: Vector of 2 measurement outcomes for phase-flip errors

Returns: Updated circuit with correction applied

Apply correction based on Shor code syndrome.

The syndrome has two parts:
- Bit-flip syndromes: 6 measurements (2 per block) indicating X errors
- Phase-flip syndromes: 2 measurements indicating Z errors between blocks

Parameters:
- circuit: Quantum circuit
- encoded-qubits: Vector of 9 qubit indices
- bit-flip-syndrome: Vector of 6 measurement outcomes for bit-flip errors
- phase-flip-syndrome: Vector of 2 measurement outcomes for phase-flip errors

Returns:
Updated circuit with correction applied
sourceraw docstring

correct-steane-errorclj

(correct-steane-error circuit encoded-qubits syndrome)

Apply correction based on Steane code syndrome.

The syndrome is a 6-bit vector [x0 x1 x2 z0 z1 z2] where:

  • [x0 x1 x2] identifies X errors (bit-flips)
  • [z0 z1 z2] identifies Z errors (phase-flips)

The syndrome bits form a binary number indicating which qubit has an error:

  • For X errors: bits [x2 x1 x0] give the qubit index (1-7)
  • For Z errors: bits [z2 z1 z0] give the qubit index (1-7)
  • Syndrome 0 means no error

Parameters:

  • circuit: Quantum circuit
  • encoded-qubits: Vector of 7 qubit indices containing the encoded logical qubit
  • syndrome: Vector of 6 measurement outcomes [x0 x1 x2 z0 z1 z2]

Returns: Updated circuit with correction applied

Apply correction based on Steane code syndrome.

The syndrome is a 6-bit vector [x0 x1 x2 z0 z1 z2] where:
- [x0 x1 x2] identifies X errors (bit-flips)
- [z0 z1 z2] identifies Z errors (phase-flips)

The syndrome bits form a binary number indicating which qubit has an error:
- For X errors: bits [x2 x1 x0] give the qubit index (1-7)
- For Z errors: bits [z2 z1 z0] give the qubit index (1-7)
- Syndrome 0 means no error

Parameters:
- circuit: Quantum circuit
- encoded-qubits: Vector of 7 qubit indices containing the encoded logical qubit
- syndrome: Vector of 6 measurement outcomes [x0 x1 x2 z0 z1 z2]

Returns:
Updated circuit with correction applied
sourceraw docstring

decode-bit-flipclj

(decode-bit-flip circuit encoded-qubits)

Decode a 3-qubit bit-flip code back to a logical qubit.

The decoding circuit is the inverse of the encoding: applies two CNOT gates in reverse order.

Parameters:

  • circuit: Quantum circuit with encoded qubits
  • encoded-qubits: Vector of 3 qubit indices [q0 q1 q2]

Returns: Updated circuit with decoding operations, logical qubit is in q0

Decode a 3-qubit bit-flip code back to a logical qubit.

The decoding circuit is the inverse of the encoding:
applies two CNOT gates in reverse order.

Parameters:
- circuit: Quantum circuit with encoded qubits
- encoded-qubits: Vector of 3 qubit indices [q0 q1 q2]

Returns:
Updated circuit with decoding operations, logical qubit is in q0
sourceraw docstring

decode-five-qubitclj

(decode-five-qubit circuit encoded-qubits)

Decode a 5-qubit perfect code back to a logical qubit.

The decoding circuit is the inverse of the encoding circuit, applying the same gates in reverse order.

Parameters:

  • circuit: Quantum circuit with encoded qubits
  • encoded-qubits: Vector of 5 qubit indices [q0 q1 q2 q3 q4]

Returns: Updated circuit with decoding operations, logical qubit is in q0

Decode a 5-qubit perfect code back to a logical qubit.

The decoding circuit is the inverse of the encoding circuit,
applying the same gates in reverse order.

Parameters:
- circuit: Quantum circuit with encoded qubits
- encoded-qubits: Vector of 5 qubit indices [q0 q1 q2 q3 q4]

Returns:
Updated circuit with decoding operations, logical qubit is in q0
sourceraw docstring

decode-shorclj

(decode-shor circuit encoded-qubits)

Decode a 9-qubit Shor code back to a logical qubit.

The decoding reverses the encoding operations:

  1. Undo bit-flip encoding in each block
  2. Undo Hadamards
  3. Undo phase-flip encoding

Parameters:

  • circuit: Quantum circuit with encoded qubits
  • encoded-qubits: Vector of 9 qubit indices

Returns: Updated circuit with decoding operations, logical qubit is in first position

Decode a 9-qubit Shor code back to a logical qubit.

The decoding reverses the encoding operations:
1. Undo bit-flip encoding in each block
2. Undo Hadamards
3. Undo phase-flip encoding

Parameters:
- circuit: Quantum circuit with encoded qubits
- encoded-qubits: Vector of 9 qubit indices

Returns:
Updated circuit with decoding operations, logical qubit is in first position
sourceraw docstring

decode-steaneclj

(decode-steane circuit encoded-qubits)

Decode a 7-qubit Steane code back to a logical qubit.

The decoding circuit is the inverse of the encoding circuit, applying the same gates in reverse order.

Parameters:

  • circuit: Quantum circuit with encoded qubits
  • encoded-qubits: Vector of 7 qubit indices [q0 q1 q2 q3 q4 q5 q6]

Returns: Updated circuit with decoding operations, logical qubit is in q0

Decode a 7-qubit Steane code back to a logical qubit.

The decoding circuit is the inverse of the encoding circuit,
applying the same gates in reverse order.

Parameters:
- circuit: Quantum circuit with encoded qubits
- encoded-qubits: Vector of 7 qubit indices [q0 q1 q2 q3 q4 q5 q6]

Returns:
Updated circuit with decoding operations, logical qubit is in q0
sourceraw docstring

decode-syndrome-bitsclj

(decode-syndrome-bits syndrome-bits code-key syndrome-table)

Decode syndrome bits to identify detected errors.

Interprets syndrome measurements to determine which physical qubits have errors. Uses the syndrome table from the error correction code.

Parameters:

  • syndrome-bits: Vector of syndrome measurement outcomes
  • code-key: Error correction code keyword (:bit-flip or :shor)
  • syndrome-table: Lookup table mapping syndromes to error patterns

Returns: Map with error information: {:syndrome syndrome-bits :error-detected? boolean :error-location string (Pauli operator indicating error) :error-type :x or :z or :both :correction-needed? boolean}

Example: Bit-flip syndrome [1 0] → {:error-location "XII", :error-type :x}

Decode syndrome bits to identify detected errors.

Interprets syndrome measurements to determine which physical qubits
have errors. Uses the syndrome table from the error correction code.

Parameters:
- syndrome-bits: Vector of syndrome measurement outcomes
- code-key: Error correction code keyword (:bit-flip or :shor)
- syndrome-table: Lookup table mapping syndromes to error patterns

Returns:
Map with error information:
{:syndrome syndrome-bits
 :error-detected? boolean
 :error-location string (Pauli operator indicating error)
 :error-type :x or :z or :both
 :correction-needed? boolean}

Example:
Bit-flip syndrome [1 0] → {:error-location "XII", :error-type :x}
sourceraw docstring

encode-bit-flipclj

(encode-bit-flip circuit logical-qubit)

Encode a logical qubit into the 3-qubit bit-flip code.

The encoding circuit applies two CNOT gates to create the encoded state:

  • |0⟩ → |000⟩
  • |1⟩ → |111⟩

Parameters:

  • circuit: Quantum circuit
  • logical-qubit: Index of the logical qubit to encode (will use qubits logical-qubit, logical-qubit+1, logical-qubit+2)

Returns: Updated circuit with encoding operations added

Encode a logical qubit into the 3-qubit bit-flip code.

The encoding circuit applies two CNOT gates to create the encoded state:
- |0⟩ → |000⟩
- |1⟩ → |111⟩

Parameters:
- circuit: Quantum circuit
- logical-qubit: Index of the logical qubit to encode (will use qubits logical-qubit, logical-qubit+1, logical-qubit+2)

Returns:
Updated circuit with encoding operations added
sourceraw docstring

encode-five-qubitclj

(encode-five-qubit circuit logical-qubit)

Encode a logical qubit into the 5-qubit perfect code.

The 5-qubit code is the smallest quantum error-correcting code that can protect against arbitrary single-qubit errors. It encodes 1 logical qubit into 5 physical qubits with distance 3.

The encoding circuit uses a specific sequence of CNOT and Hadamard gates that creates the stabilizer code space spanned by the four stabilizer generators:

  • XZZXI
  • IXZZX
  • XIXZZ
  • ZXIXZ

Parameters:

  • circuit: Quantum circuit
  • logical-qubit: Index of the first qubit (will use qubits logical-qubit to logical-qubit+4)

Returns: Updated circuit with 5-qubit code encoding operations

Encode a logical qubit into the 5-qubit perfect code.

The 5-qubit code is the smallest quantum error-correcting code that can
protect against arbitrary single-qubit errors. It encodes 1 logical qubit
into 5 physical qubits with distance 3.

The encoding circuit uses a specific sequence of CNOT and Hadamard gates
that creates the stabilizer code space spanned by the four stabilizer generators:
- XZZXI
- IXZZX
- XIXZZ
- ZXIXZ

Parameters:
- circuit: Quantum circuit
- logical-qubit: Index of the first qubit (will use qubits logical-qubit to logical-qubit+4)

Returns:
Updated circuit with 5-qubit code encoding operations
sourceraw docstring

encode-shorclj

(encode-shor circuit logical-qubit)

Encode a logical qubit into the 9-qubit Shor code.

The Shor code combines bit-flip and phase-flip protection:

  1. First, apply phase-flip encoding (creates 3 qubits in superposition)
  2. Then, apply bit-flip encoding to each of the 3 blocks

Encoding:

  • |0⟩ → 1/2√2 [(|000⟩+|111⟩) ⊗ (|000⟩+|111⟩) ⊗ (|000⟩+|111⟩)]
  • |1⟩ → 1/2√2 [(|000⟩-|111⟩) ⊗ (|000⟩-|111⟩) ⊗ (|000⟩-|111⟩)]

Parameters:

  • circuit: Quantum circuit
  • logical-qubit: Index of the first qubit (will use qubits 0-8)

Returns: Updated circuit with Shor encoding operations

Encode a logical qubit into the 9-qubit Shor code.

The Shor code combines bit-flip and phase-flip protection:
1. First, apply phase-flip encoding (creates 3 qubits in superposition)
2. Then, apply bit-flip encoding to each of the 3 blocks

Encoding:
- |0⟩ → 1/2√2 [(|000⟩+|111⟩) ⊗ (|000⟩+|111⟩) ⊗ (|000⟩+|111⟩)]
- |1⟩ → 1/2√2 [(|000⟩-|111⟩) ⊗ (|000⟩-|111⟩) ⊗ (|000⟩-|111⟩)]

Parameters:
- circuit: Quantum circuit
- logical-qubit: Index of the first qubit (will use qubits 0-8)

Returns:
Updated circuit with Shor encoding operations
sourceraw docstring

encode-steaneclj

(encode-steane circuit logical-qubit)

Encode a logical qubit into the 7-qubit Steane code.

The Steane code is a CSS code derived from the classical [7,4] Hamming code. It can correct any single-qubit error using only 7 physical qubits.

The encoding circuit implements the generator matrix of the [7,4,3] Hamming code:

  • Starts with logical qubit in position 0
  • Applies CNOT gates according to the parity-check matrix structure
  • Creates an encoded state that spans the stabilizer code space

Encoding circuit structure:

  1. Apply CNOTs from qubit 0 to create X-type stabilizers
  2. Apply Hadamards to transform to Z-type stabilizers (CSS structure)
  3. Final CNOTs complete the encoding

Parameters:

  • circuit: Quantum circuit
  • logical-qubit: Index of the first qubit (will use qubits logical-qubit to logical-qubit+6)

Returns: Updated circuit with Steane encoding operations

Encode a logical qubit into the 7-qubit Steane code.

The Steane code is a CSS code derived from the classical [7,4] Hamming code.
It can correct any single-qubit error using only 7 physical qubits.

The encoding circuit implements the generator matrix of the [7,4,3] Hamming code:
- Starts with logical qubit in position 0
- Applies CNOT gates according to the parity-check matrix structure
- Creates an encoded state that spans the stabilizer code space

Encoding circuit structure:
1. Apply CNOTs from qubit 0 to create X-type stabilizers
2. Apply Hadamards to transform to Z-type stabilizers (CSS structure)
3. Final CNOTs complete the encoding

Parameters:
- circuit: Quantum circuit
- logical-qubit: Index of the first qubit (will use qubits logical-qubit to logical-qubit+6)

Returns:
Updated circuit with Steane encoding operations
sourceraw docstring

extract-syndrome-bitsclj

(extract-syndrome-bits results ctx)

Extract syndrome measurement bits from quantum circuit results. Maps ancilla measurements to original logical qubits using reverse mappings.

The syndrome bits are stored in the measurement results of the ancilla qubits. This function identifies which measurement results correspond to syndrome measurements and maps them back to the original circuit's logical qubits.

Parameters:

  • results: Map containing measurement results from circuit execution {:measurements {qubit-idx value, ...}, :measurement-counts {...}, ...}
  • ctx: Optimization context with error correction metadata and reverse mappings {:logical-to-ancillas {compacted-logical [ancilla...], ...} :inverse-qubit-mapping {compacted-logical original-logical, ...}}

Returns: Map from ORIGINAL logical qubit index to syndrome bits {original-logical-0 [1 0], original-logical-2 [0 1], ...}

Example: Original circuit has qubits 0, 2 (1 unused). After optimization:

  • Compacted 0 → Original 0, ancillas [6 7]
  • Compacted 1 → Original 2, ancillas [8 9]
  • Measurements {6 1, 7 0, 8 0, 9 1}
  • Result: {0 [1 0], 2 [0 1]}
Extract syndrome measurement bits from quantum circuit results.
Maps ancilla measurements to original logical qubits using reverse mappings.

The syndrome bits are stored in the measurement results of the ancilla qubits.
This function identifies which measurement results correspond to syndrome
measurements and maps them back to the original circuit's logical qubits.

Parameters:
- results: Map containing measurement results from circuit execution
  {:measurements {qubit-idx value, ...}, :measurement-counts {...}, ...}
- ctx: Optimization context with error correction metadata and reverse mappings
  {:logical-to-ancillas {compacted-logical [ancilla...], ...}
   :inverse-qubit-mapping {compacted-logical original-logical, ...}}

Returns:
Map from ORIGINAL logical qubit index to syndrome bits
{original-logical-0 [1 0], original-logical-2 [0 1], ...}

Example:
Original circuit has qubits 0, 2 (1 unused). After optimization:
- Compacted 0 → Original 0, ancillas [6 7]
- Compacted 1 → Original 2, ancillas [8 9]
- Measurements {6 1, 7 0, 8 0, 9 1}
- Result: {0 [1 0], 2 [0 1]}
sourceraw docstring

five-qubit-codeclj

The 5-qubit perfect code is the smallest code that can correct arbitrary single-qubit errors.

This code is optimal in the sense that it uses the minimum number of physical qubits (5) required to encode 1 logical qubit with distance 3.

Distance: 3 (can correct any single-qubit error)

The 5-qubit perfect code is the smallest code that can correct arbitrary single-qubit errors.

This code is optimal in the sense that it uses the minimum number of physical qubits
(5) required to encode 1 logical qubit with distance 3.

Distance: 3 (can correct any single-qubit error)
sourceraw docstring

get-codeclj

(get-code code-key)

Retrieve a predefined error correction code by keyword.

Parameters:

  • code-key: Keyword identifying the code (e.g., :bit-flip, :shor)

Returns: Stabilizer code definition, or nil if not found

Retrieve a predefined error correction code by keyword.

Parameters:
- code-key: Keyword identifying the code (e.g., :bit-flip, :shor)

Returns:
Stabilizer code definition, or nil if not found
sourceraw docstring

inject-syndrome-measurementsclj

(inject-syndrome-measurements circuit
                              code-key
                              logical-to-physical
                              logical-to-ancillas)

Add syndrome measurement operations to an error-corrected circuit.

This function adds the syndrome measurement gates and operations that detect errors in the encoded qubits. The syndrome measurements are non-destructive: they detect errors without collapsing the quantum state of the logical qubits.

For bit-flip code:

  • Adds CNOT gates to measure Z₀Z₁ and Z₁Z₂ stabilizers
  • Measures 2 ancilla qubits per logical qubit

For Shor code:

  • Adds bit-flip syndrome measurements (6 ancillas per logical qubit)
  • Adds phase-flip syndrome measurements (2 ancillas per logical qubit)
  • Total: 8 ancilla measurements per logical qubit

Parameters:

  • circuit: Circuit with encoded qubits
  • code-key: Error correction code (:bit-flip or :shor)
  • logical-to-physical: Map from logical qubit indices to physical qubit vectors
  • logical-to-ancillas: Map from logical qubit indices to ancilla qubit vectors

Returns: Updated circuit with syndrome measurement operations added

Add syndrome measurement operations to an error-corrected circuit.

This function adds the syndrome measurement gates and operations that detect
errors in the encoded qubits. The syndrome measurements are non-destructive:
they detect errors without collapsing the quantum state of the logical qubits.

For bit-flip code:
- Adds CNOT gates to measure Z₀Z₁ and Z₁Z₂ stabilizers
- Measures 2 ancilla qubits per logical qubit

For Shor code:
- Adds bit-flip syndrome measurements (6 ancillas per logical qubit)
- Adds phase-flip syndrome measurements (2 ancillas per logical qubit)
- Total: 8 ancilla measurements per logical qubit

Parameters:
- circuit: Circuit with encoded qubits
- code-key: Error correction code (:bit-flip or :shor)
- logical-to-physical: Map from logical qubit indices to physical qubit vectors
- logical-to-ancillas: Map from logical qubit indices to ancilla qubit vectors

Returns:
Updated circuit with syndrome measurement operations added
sourceraw docstring

list-available-codesclj

(list-available-codes)

List all available error correction codes.

Returns: Vector of maps with :key, :name, and :description for each code

List all available error correction codes.

Returns:
Vector of maps with :key, :name, and :description for each code
sourceraw docstring

measure-bit-flip-syndromeclj

(measure-bit-flip-syndrome circuit encoded-qubits ancilla-qubits)

Measure the syndrome for the 3-qubit bit-flip code.

This adds ancilla qubits and measurement operations to detect errors without destroying the encoded quantum information.

The syndrome measurements check:

  • Z₀Z₁ (compares first two qubits)
  • Z₁Z₂ (compares last two qubits)

Parameters:

  • circuit: Quantum circuit with encoded qubits
  • encoded-qubits: Vector of 3 qubit indices [q0 q1 q2] containing the encoded logical qubit
  • ancilla-qubits: Vector of 2 ancilla qubit indices for syndrome measurement

Returns: Updated circuit with syndrome measurement operations

Measure the syndrome for the 3-qubit bit-flip code.

This adds ancilla qubits and measurement operations to detect errors
without destroying the encoded quantum information.

The syndrome measurements check:
- Z₀Z₁ (compares first two qubits)
- Z₁Z₂ (compares last two qubits)

Parameters:
- circuit: Quantum circuit with encoded qubits
- encoded-qubits: Vector of 3 qubit indices [q0 q1 q2] containing the encoded logical qubit
- ancilla-qubits: Vector of 2 ancilla qubit indices for syndrome measurement

Returns:
Updated circuit with syndrome measurement operations
sourceraw docstring

measure-five-qubit-syndromeclj

(measure-five-qubit-syndrome circuit encoded-qubits ancilla-qubits)

Measure the syndrome for the 5-qubit perfect code.

The 5-qubit code has 4 stabilizer generators, requiring 4 ancilla qubits for syndrome measurement:

  • XZZXI
  • IXZZX
  • XIXZZ
  • ZXIXZ

Parameters:

  • circuit: Quantum circuit with encoded qubits
  • encoded-qubits: Vector of 5 qubit indices for the encoded logical qubit
  • ancilla-qubits: Vector of 4 ancilla qubit indices

Returns: Updated circuit with syndrome measurement operations

Measure the syndrome for the 5-qubit perfect code.

The 5-qubit code has 4 stabilizer generators, requiring 4 ancilla qubits
for syndrome measurement:
- XZZXI
- IXZZX
- XIXZZ
- ZXIXZ

Parameters:
- circuit: Quantum circuit with encoded qubits
- encoded-qubits: Vector of 5 qubit indices for the encoded logical qubit
- ancilla-qubits: Vector of 4 ancilla qubit indices

Returns:
Updated circuit with syndrome measurement operations
sourceraw docstring

measure-shor-syndromeclj

(measure-shor-syndrome circuit
                       encoded-qubits
                       bit-flip-ancillas
                       phase-flip-ancillas)

Measure the syndrome for the 9-qubit Shor code.

The Shor code requires:

  • 6 ancilla qubits for bit-flip syndrome (2 per block)
  • 2 ancilla qubits for phase-flip syndrome Total: 8 ancilla qubits

Parameters:

  • circuit: Quantum circuit with encoded qubits
  • encoded-qubits: Vector of 9 qubit indices for the encoded logical qubit
  • bit-flip-ancillas: Vector of 6 ancilla qubit indices for bit-flip detection
  • phase-flip-ancillas: Vector of 2 ancilla qubit indices for phase-flip detection

Returns: Updated circuit with syndrome measurement operations

Measure the syndrome for the 9-qubit Shor code.

The Shor code requires:
- 6 ancilla qubits for bit-flip syndrome (2 per block)
- 2 ancilla qubits for phase-flip syndrome
Total: 8 ancilla qubits

Parameters:
- circuit: Quantum circuit with encoded qubits
- encoded-qubits: Vector of 9 qubit indices for the encoded logical qubit
- bit-flip-ancillas: Vector of 6 ancilla qubit indices for bit-flip detection
- phase-flip-ancillas: Vector of 2 ancilla qubit indices for phase-flip detection

Returns:
Updated circuit with syndrome measurement operations
sourceraw docstring

measure-steane-syndromeclj

(measure-steane-syndrome circuit encoded-qubits ancilla-qubits)

Measure the syndrome for the 7-qubit Steane code.

The Steane code has 6 stabilizer generators (3 X-type, 3 Z-type), requiring 6 ancilla qubits for syndrome measurement.

The syndrome measurements check:

  • 3 X-type stabilizers: IIIXXXX, IXXIIXX, XIXIXIX
  • 3 Z-type stabilizers: IIIZZZZ, IZZIIZZ, ZIZIZIZ

Parameters:

  • circuit: Quantum circuit with encoded qubits
  • encoded-qubits: Vector of 7 qubit indices for the encoded logical qubit
  • ancilla-qubits: Vector of 6 ancilla qubit indices (3 for X, 3 for Z)

Returns: Updated circuit with syndrome measurement operations

Measure the syndrome for the 7-qubit Steane code.

The Steane code has 6 stabilizer generators (3 X-type, 3 Z-type),
requiring 6 ancilla qubits for syndrome measurement.

The syndrome measurements check:
- 3 X-type stabilizers: IIIXXXX, IXXIIXX, XIXIXIX
- 3 Z-type stabilizers: IIIZZZZ, IZZIIZZ, ZIZIZIZ

Parameters:
- circuit: Quantum circuit with encoded qubits
- encoded-qubits: Vector of 7 qubit indices for the encoded logical qubit
- ancilla-qubits: Vector of 6 ancilla qubit indices (3 for X, 3 for Z)

Returns:
Updated circuit with syndrome measurement operations
sourceraw docstring

phase-flip-codeclj

The 3-qubit phase-flip code protects against single phase-flip (Z) errors.

Encoding:

  • |0⟩ → |+++⟩ = (|000⟩+|111⟩)/√2
  • |1⟩ → |---⟩ = (|000⟩-|111⟩)/√2

Stabilizer generators: X₀X₁, X₁X₂

This code can detect and correct a single phase-flip error on any qubit.

The 3-qubit phase-flip code protects against single phase-flip (Z) errors.

Encoding:
- |0⟩ → |+++⟩ = (|000⟩+|111⟩)/√2
- |1⟩ → |---⟩ = (|000⟩-|111⟩)/√2

Stabilizer generators: X₀X₁, X₁X₂

This code can detect and correct a single phase-flip error on any qubit.
sourceraw docstring

shor-codeclj

The 9-qubit Shor code protects against arbitrary single-qubit errors.

This code combines the bit-flip and phase-flip codes to protect against both X and Z errors (and thus arbitrary errors by linearity).

Stabilizer generators:

  • 6 Z-type stabilizers (for bit-flip detection in 3 blocks)
  • 2 X-type stabilizers (for phase-flip detection between blocks)

Distance: 3 (can correct any single-qubit error)

The 9-qubit Shor code protects against arbitrary single-qubit errors.

This code combines the bit-flip and phase-flip codes to protect against
both X and Z errors (and thus arbitrary errors by linearity).

Stabilizer generators:
- 6 Z-type stabilizers (for bit-flip detection in 3 blocks)
- 2 X-type stabilizers (for phase-flip detection between blocks)

Distance: 3 (can correct any single-qubit error)
sourceraw docstring

steane-codeclj

The 7-qubit Steane code is a CSS code derived from the classical [7,4] Hamming code.

This is one of the most efficient codes for correcting arbitrary single-qubit errors, using only 7 physical qubits to encode 1 logical qubit.

Distance: 3 (can correct any single-qubit error)

The 7-qubit Steane code is a CSS code derived from the classical [7,4] Hamming code.

This is one of the most efficient codes for correcting arbitrary single-qubit errors,
using only 7 physical qubits to encode 1 logical qubit.

Distance: 3 (can correct any single-qubit error)
sourceraw docstring

syndrome-weightclj

(syndrome-weight syndrome)

Calculate the weight of a syndrome (number of non-zero measurements).

Parameters:

  • syndrome: Vector of 0s and 1s

Returns: Integer count of 1s in syndrome

Calculate the weight of a syndrome (number of non-zero measurements).

Parameters:
- syndrome: Vector of 0s and 1s

Returns:
Integer count of 1s in syndrome
sourceraw docstring

trivial-syndrome?clj

(trivial-syndrome? syndrome)

Check if a syndrome indicates no errors (all zeros).

Parameters:

  • syndrome: Vector of 0s and 1s

Returns: Boolean indicating if syndrome is all zeros

Check if a syndrome indicates no errors (all zeros).

Parameters:
- syndrome: Vector of 0s and 1s

Returns:
Boolean indicating if syndrome is all zeros
sourceraw docstring

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
Move to previous article
Move to next article
Ctrl+/Jump to the search field
× close