Skip to content

Fix tests to account for QEC and box ops in transpilation#51

Open
conorfitzmaurice wants to merge 2 commits into
mainfrom
test-updates
Open

Fix tests to account for QEC and box ops in transpilation#51
conorfitzmaurice wants to merge 2 commits into
mainfrom
test-updates

Conversation

@conorfitzmaurice
Copy link
Copy Markdown

@conorfitzmaurice conorfitzmaurice commented Apr 20, 2026

  • Switched the test module from LogicalCircuitGeneral to LogicalCircuit so that LogicalStatevector (which isinstance-checks against LogicalCircuit) and the rotation gates (rx, ry, rz, rxx, ryy, rzz) are usable.
  • Added 3 shared helpers:
  1. TestSingleQubitGate: encodes 0 and 1, applies logical gate, builds LogicalStatevector, and compares its logical_decomposition against Statevector.from_int(...).evolve(reference_gate).
  2. TestSingleQubitRotationGate: same flow over sweep of theta values, with relaxed tolerance >= 0.99 for the Solovay-Kitaev approximation used by LogicalCircuit.rx/ry/rz.
  3. TestMultiQubitGateConstruction: construction-only sanity test for two-logical-qubit gates
  • Implemented the gate tests on top of helpers:
  1. Single-qubit fidelity tests: TestX, TestY, TestZ, TestH, TestS, TestSdg, TestT, TestTdg.
  2. Single-qubit rotation fidelity tests: TestRX, TestRY, TestRZ.
  3. Multi-qubit construction tests: TestCX, TestRXX, TestRYY, TestRZZ.
  • Added TestSdg and TestTdg to Clifford / non-Clifford umbrellas.
  • TestAllGates now actually runs TestCliffordGates, TestNonCliffordGates, and TestRotationGates.
  • Non-Steane QECCs are skipped with warning for fidelity-based tests, mirroring existing limitation in TestStateRepresentations.py.

Update:
Added new LogicalQ/tests/init.py so fidelity tolerances are shared test configuration rather than global hardcoded variables:

  • FIDELITY_ATOL: derived from inspect.signature(np.isclose).parameters["atol"].default, so it tracks numpy's own default instead of being hardcoded.
  • sk_rotation_atol(recursion_degree, depth, basis_gates, thetas): measures the worst-case Solovay-Kitaev state infidelity for RXGate/RYGate/RZGate at the supplied parameters/angles and returns it as the tolerance.
  • TestSingleQubitRotationGate passes recursion_degree, depth, and thetas through, so tolerance always matches the approximation regime of test being run. Overridable via atol kwarg.

Tests

Ran locally against qiskit==2.4.0, qiskit-aer==0.17.2, qiskit-addon-utils==0.3.1.
Functional

  • TestAllGates() with default implemented_codes: True
  • TestAllGates([steane_code]): True (every test, no warnings)
  • TestAllGates([five_qubit_code]): True (fidelity tests skipped with warnings, construction tests still execute)
    Cross-validation against demo tooling
  • Single-qubit fidelity matches logical_state_fidelity(lsv, Statevector(qc)): the pattern for demos/RGateValidation.ipynb and demos/LogicalGateBenchmarking.ipynb: to 1e-8 for all 16 {X, Y, Z, H, S, Sdg, T, Tdg} x {0, 1} cases.
  • Rotation fidelity matches the same demo pattern to 1e-8 for all 15 {RX, RY, RZ} x {5 angles} cases; measured fidelities span 0.9921–1.0000, all passing the derived sk_rotation_atol bound (~0.006 for the 5-angle test sweep).
    Configuration override
  • Overriding atol=0.0 via kwarg correctly makes non-identity rotation cases report False, inheriting tolerance.
    Negative controls (to confirm the suite catches regressions rather than always reporting True)
  • Claiming X = Z: returns False (fidelity 0.0 for both 0 and 1)
  • Claiming H = X: returns False (fidelity 0.5)
  • Claiming RX(theta) = RX(2*theta): returns False for all theta not equal to 0
  • Construction helper with a raising lqc_method: returns False for every QECC

Tolerance derivation check

  • At each test angle theta, the infidelity observed on the logical circuit matches the infidelity sk_rotation_atol measures on the physical SK decomposition (e.g. RX at theta=1.2566: 0.006000 in both).

Known limitations (not regressions)

  • Multi-qubit gate correctness (CX, RXX, RYY, RZZ) is not fidelity-tested: same intractability as the demo's caveat.
  • Non-Steane QECCs are skipped for fidelity tests until LogicalStatevector supports them.

@RasmitDevkota
Copy link
Copy Markdown
Member

A few notes that need to be addressed right away:

  • Logical fidelity should be computed using the existing tools
  • Tolerances should be inherited
  • Limit the use of helper functions, especially when they're only two lines long and only complicate the readability of code
  • What testing was performed to show that this is all correct?

Comment thread LogicalQ/tests/TestLogicalGates.py Outdated
Comment thread LogicalQ/tests/TestLogicalGates.py Outdated
Comment thread LogicalQ/tests/TestLogicalGates.py Outdated
Comment thread LogicalQ/tests/TestLogicalGates.py Outdated
Comment thread LogicalQ/tests/TestLogicalGates.py Outdated
fidelity = _logical_fidelity(lsv.logical_decomposition, sv_expected.data)

lqc_h.measure_all()
if np.isclose(fidelity, 1.0, atol=GATE_FIDELITY_ATOL):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're not sure how logging should be handled, we should discuss this

Added new LogicalQ/tests/__init__.py so fidelity tolerances are shared test configuration rather than global hardcoded variables:
- FIDELITY_ATOL: derived from inspect.signature(np.isclose).parameters["atol"].default, so it tracks numpy's own default instead of being hardcoded.
- sk_rotation_atol(recursion_degree, depth, basis_gates, thetas): measures the worst-case Solovay-Kitaev state infidelity for RXGate/RYGate/RZGate at the supplied parameters/angles and returns it as the tolerance. TestSingleQubitRotationGate passes recursion_degree, depth, and thetas through, so tolerance always matches the approximation regime of test being run. Overridable via atol kwarg.
No production (non-test) code was modified.
## Tests
Ran locally against qiskit==2.4.0, qiskit-aer==0.17.2, qiskit-addon-utils==0.3.1.
**Functional**
- [x] TestAllGates() with default implemented_codes: True
- [x] TestAllGates([steane_code]): True (every test, no warnings)
- [x] TestAllGates([five_qubit_code]): True (fidelity tests skipped with warnings, construction tests still execute)
**Cross-validation against demo tooling**
- [x] Single-qubit fidelity matches logical_state_fidelity(lsv, Statevector(qc)): the pattern for demos/RGateValidation.ipynb and demos/LogicalGateBenchmarking.ipynb: to 1e-8 for all 16 {X, Y, Z, H, S, Sdg, T, Tdg} x {0, 1} cases.
- [x] Rotation fidelity matches the same demo pattern to 1e-8 for all 15 {RX, RY, RZ} x {5 angles} cases; measured fidelities span 0.9921–1.0000, all passing the derived sk_rotation_atol bound (~0.006 for the 5-angle test sweep).
**Configuration override**
- [x] Overriding atol=0.0 via kwarg correctly makes non-identity rotation cases report False, confirming the inherited tolerance is actually honored (not silently ignored).
**Negative controls** (to confirm the suite catches regressions rather than always reporting True)
- [x] Claiming X = Z: returns False (fidelity 0.0 for both 0 and 1)
- [x] Claiming H = X: returns False (fidelity 0.5)
- [x] Claiming RX(theta) = RX(2*theta): returns False for all theta not equal to 0
- [x] Construction helper with a raising lqc_method: returns False for every QECC
## Tolerance derivation check
- At each test angle theta, the infidelity observed on the logical circuit matches the infidelity sk_rotation_atol measures on the physical SK decomposition (e.g. RX at θ=1.2566: 0.006000 in both).
## Known limitations (not regressions)
- Multi-qubit gate correctness (CX, RXX, RYY, RZZ) is not fidelity-tested: same intractability as the demo's caveat.
- Non-Steane QECCs are skipped for fidelity tests until LogicalStatevector supports them.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants