Skip to content

QCDL operations: add reset and sxdg, and validate qubit arguments - #72

Open
qci-amos wants to merge 5 commits into
mainfrom
qcdl/operations-reset-sxdg-and-validation
Open

QCDL operations: add reset and sxdg, and validate qubit arguments#72
qci-amos wants to merge 5 commits into
mainfrom
qcdl/operations-reset-sxdg-and-validation

Conversation

@qci-amos

Copy link
Copy Markdown
Collaborator

Part of splitting #71 into reviewable pieces. This one is the operations
module; the other four PRs are independent of it and of each other.

Two commits, reviewable separately.

1. Add reset and sxdg operations

reset already existed as q0.reset(), but only through __getattr__, so it
had no signature, no docstring, and no importable name — the user guide reached
for it in a way nothing else in the guide does. This adds the operation form.
It is an alias for the same statement, not a variant (there is a test asserting
the two produce identical programs).

Unlike initialize, which loops until every qubit in the program is reset,
reset acts on one qubit and has a deterministic duration, so it can be used
inside a conditional branch. The guide's erasure-detection example now uses it.

sxdg is the adjoint of sx and was simply missing from the gate set.

2. Validate the qubit arguments of every operation

Two failure modes were unpleasant:

  • Passing something that is not a qubit surfaced much later as
    AttributeError: 'int' object has no attribute 'procedure', naming an
    internal attribute rather than the argument at fault.
  • A two-qubit gate given the same qubit twice — cx(q0, q0) — built cleanly
    and was rejected only once the program reached the service.

_validate_qubit_args wraps every operation and derives the rule from the
signature rather than from a hand-maintained list:

  • each named QCDLModule parameter is a separate role in the operation, so
    two of them may not be the same module;
  • a *qubits parameter is a set of qubits to act on, where a repeat is
    harmless (barrier, initialize).

A test asserts those two categories stay disjoint and cover every multi-qubit
operation, so a new operation lands in one of them rather than escaping the
check. Another asserts every operation is actually wrapped.

The wrapper preserves __name__, __doc__, __module__ and the signature, so
autodoc is unaffected, and it defers to Python for arity errors so
cx(q0) still reports against the real signature.

Also adds __all__, so from dwave.gate.qcdl.operations import * brings in the
operations rather than this module's own imports (np, inspect,
implementations, ...).

Compatibility

Code that passed a non-qubit, or the same qubit twice to a two-qubit gate, now
raises QCDLUserError at build time instead of failing later or being rejected
by the service. Nothing that built a valid program changes.

Testing

pytest tests/ passes. New tests are parameterized over every operation
discovered by introspection, so they cover gates added later too.

🤖 Generated with Claude Code

qci-amos and others added 2 commits August 19, 2026 12:50
reset(q0) is the operation form of the q0.reset() statement, which was
only reachable through __getattr__ and so had no signature, docstring, or
importable name. Unlike initialize(), it acts on one qubit and has a
deterministic duration, so it is usable inside a conditional branch. The
user guide now uses it.

sxdg is the adjoint of sx, which was missing from the gate set.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Passing something that is not a qubit surfaced much later as an
AttributeError naming an internal attribute, and a two-qubit gate given
the same qubit twice built cleanly and was rejected only by the service.

_validate_qubit_args wraps every operation and derives the rule from the
signature: each named QCDLModule parameter is a separate role, so two of
them may not be the same module, whereas a *qubits parameter is a set of
qubits where a repeat is harmless. The wrapper keeps the signature and
docstring, and defers to python for arity errors.

Also adds __all__, so `from ... import *` brings in the operations rather
than this module's own imports.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@codecov

codecov Bot commented Aug 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.53%. Comparing base (206a2d6) to head (91f1b72).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #72      +/-   ##
==========================================
+ Coverage   90.18%   90.53%   +0.35%     
==========================================
  Files          31       31              
  Lines        5317     5514     +197     
==========================================
+ Hits         4795     4992     +197     
  Misses        522      522              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread docs/workflow.rst

from dwave.gate.qcdl import qcdl
from dwave.gate.qcdl.operations import initialize
from dwave.gate.qcdl.operations import reset

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Good catch, thanks!

Comment thread docs/workflow.rst
@qcdl(1)
def detect_erasure_example(q):
erased = q.Register(name="erased")
def detect_erasure_example(q0):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Another good catch

Comment thread dwave/gate/qcdl/operations.py Outdated
qci-amos and others added 3 commits August 19, 2026 17:14
Co-authored-by: Joel Pasvolsky <34041130+JoelPasvolsky@users.noreply.github.com>
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