-
Notifications
You must be signed in to change notification settings - Fork 32
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
Improve separate_circuit
docstring
#267
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f8d9e0e
Improve `separate_circuit` docstring
garrison ead01aa
Fix doctest discovery when DOcplex is not available
garrison 6ede313
Update circuit_knitting/utils/transforms.py
garrison 1325359
Merge branch 'main' into improve-separate_circuit-docstring
garrison File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,11 @@ | |
:toctree: ../stubs | ||
|
||
separate_circuit | ||
|
||
.. autosummary:: | ||
:toctree: ../stubs | ||
:template: autosummary/class_no_inherited_members.rst | ||
|
||
SeparatedCircuits | ||
""" | ||
from __future__ import annotations | ||
|
@@ -40,7 +45,14 @@ | |
|
||
|
||
class SeparatedCircuits(NamedTuple): | ||
"""Named tuple for result of :class:`separate_circuit`.""" | ||
"""Named tuple for result of :func:`separate_circuit`. | ||
|
||
``subcircuits`` is a dict of circuits, keyed by each partition label. | ||
``qubit_map`` is a list of length number of qubits in the original circuit. | ||
garrison marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Each element of that list is a 2-tuple which includes the partition label | ||
of that qubit, together with the index of that qubit in the corresponding | ||
subcircuit. | ||
""" | ||
|
||
subcircuits: dict[Hashable, QuantumCircuit] | ||
qubit_map: list[tuple[Hashable, int]] | ||
|
@@ -52,15 +64,41 @@ def separate_circuit( | |
) -> SeparatedCircuits: | ||
"""Separate the circuit into its disconnected components. | ||
|
||
If ``partition_labels`` is provided, then the circuit will be separated | ||
according to those labels. If it is ``None``, then the circuit will be | ||
fully separated into its disconnected components, each of which will be | ||
labeled with consecutive integers starting with 0. | ||
|
||
>>> qc = QuantumCircuit(4) | ||
>>> _ = qc.x(0) | ||
>>> _ = qc.cx(1, 2) | ||
>>> _ = qc.h(3) | ||
Comment on lines
+73
to
+75
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Without the
and doctest fails unless these are also added here. Of course, the memory address is not predictable, so there is no way to allow such a thing to be output and still have the tests pass. |
||
>>> separate_circuit(qc, "ABBA").subcircuits.keys() | ||
dict_keys(['A', 'B']) | ||
>>> separate_circuit(qc, "ABBA").qubit_map | ||
[('A', 0), ('B', 0), ('B', 1), ('A', 1)] | ||
>>> separate_circuit(qc, "BAAC").subcircuits.keys() | ||
dict_keys(['B', 'A', 'C']) | ||
>>> separate_circuit(qc, "BAAC").qubit_map | ||
[('B', 0), ('A', 0), ('A', 1), ('C', 0)] | ||
>>> separate_circuit(qc).subcircuits.keys() | ||
dict_keys([0, 1, 2]) | ||
>>> separate_circuit(qc).qubit_map | ||
[(0, 0), (1, 0), (1, 1), (2, 0)] | ||
|
||
Args: | ||
circuit: The circuit to separate into disconnected subcircuits | ||
partition_labels: A sequence of length ``num_qubits``. Qubits with the | ||
same label will end up in the same subcircuit. | ||
|
||
Returns: | ||
A named tuple containing the subcircuits and qubit map | ||
A :class:`SeparatedCircuits` named tuple containing the ``subcircuits`` | ||
and ``qubit_map``. | ||
|
||
Raises: | ||
ValueError: The number of partition labels does not equal the number of | ||
qubits in the input circuit. | ||
ValueError: Operation spans more than one partition. | ||
""" | ||
# Split barriers into single-qubit barriers before separating | ||
new_qc = circuit.copy() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file had to be modified for doctest discovery to work on Python 3.11 (i.e., when DOcplex is not present).