Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix documentation of SwitchCaseOp #10484

Merged
merged 1 commit into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions qiskit/circuit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,41 @@

The :class:`.SwitchCaseOp` also understands a special value:

.. py:data: CASE_DEFAULT
Used as a possible "label" in a :class:`.SwitchCaseOp` to represent the default case. This will
always match, if it is tried.
.. py:data:: CASE_DEFAULT

A special object that represents the "default" case of a switch statement. If you use this as a
case target, it must be the last case, and will match anything that wasn't already matched. For
example::

from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
from qiskit.circuit import SwitchCaseOp, CASE_DEFAULT

body0 = QuantumCircuit(2, 2)
body0.x(0)
body1 = QuantumCircuit(2, 2)
body1.z(0)
body2 = QuantumCircuit(2, 2)
body2.cx(0, 1)

qr, cr = QuantumRegister(2), ClassicalRegister(2)
qc = QuantumCircuit(qr, cr)
qc.switch(cr, [(0, body0), (1, body1), (CASE_DEFAULT, body2)], qr, cr)

When using the builder interface of :meth:`.QuantumCircuit.switch`, this can also be accessed as
the ``DEFAULT`` attribute of the bound case-builder object, such as::

from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister

qr, cr = QuantumRegister(2), ClassicalRegister(2)
qc = QuantumCircuit(qr, cr)
with qc.switch(cr) as case:
with case(0):
qc.x(0)
with case(1):
qc.z(0)
with case(case.DEFAULT):
qc.cx(0, 1)


Parametric Quantum Circuits
---------------------------
Expand Down
7 changes: 3 additions & 4 deletions qiskit/circuit/controlflow/switch_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ def __repr__(self):
"""A special object that represents the "default" case of a switch statement. If you use this
as a case target, it must be the last case, and will match anything that wasn't already matched.
When using the builder interface of :meth:`.QuantumCircuit.switch`, this can also be accessed as the
``DEFAULT`` attribute of the bound case-builder object.
"""
``DEFAULT`` attribute of the bound case-builder object."""


class SwitchCaseOp(ControlFlowOp):
Expand All @@ -51,12 +50,12 @@ class SwitchCaseOp(ControlFlowOp):
be used to represent a default condition.

This is the low-level interface for creating a switch-case statement; in general, the circuit
method :meth:`.QuantumCircuit.switch_case` should be used as a context manager to access the
method :meth:`.QuantumCircuit.switch` should be used as a context manager to access the
builder interface. At the low level, you must ensure that all the circuit blocks contain equal
numbers of qubits and clbits, and that the order the virtual bits of the containing circuit
should be bound is the same for all blocks. This will likely mean that each circuit block is
wider than its natural width, as each block must span the union of all the spaces covered by
_any_ of the blocks.
*any* of the blocks.

Args:
target: the runtime value to switch on.
Expand Down