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

Improve separate_circuit docstring #267

Merged
merged 4 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
19 changes: 10 additions & 9 deletions circuit_knitting/cutting/cutqc/mip_model.py
Copy link
Member Author

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).

Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,6 @@

import numpy as np

try:
from docplex.mp.model import Model
from docplex.mp.utils import DOcplexException
except ModuleNotFoundError as ex: # pragma: no cover
raise ModuleNotFoundError(
"DOcplex is not installed. For automatic cut finding to work, both "
"DOcplex and cplex must be available."
) from ex


class MIPModel(object):
"""
Expand Down Expand Up @@ -100,6 +91,14 @@ def __init__(
num_in_qubits += 1
self.vertex_weight[node] = num_in_qubits

try:
from docplex.mp.model import Model
except ModuleNotFoundError as ex: # pragma: no cover
raise ModuleNotFoundError(
"DOcplex is not installed. For automatic cut finding to work, both "
"DOcplex and cplex must be available."
) from ex

self.model = Model("docplex_cutter")
self.model.log_output = False
self._add_variables()
Expand Down Expand Up @@ -465,6 +464,8 @@ def solve(self, min_postprocessing_cost: float) -> bool:
# print('solving for %d subcircuits'%self.num_subcircuit)
# print('model has %d variables, %d linear constraints,%d quadratic constraints, %d general constraints'
# % (self.model.NumVars,self.model.NumConstrs, self.model.NumQConstrs, self.model.NumGenConstrs))
from docplex.mp.utils import DOcplexException

print(
"Exporting as a LP file to let you check the model that will be solved : ",
min_postprocessing_cost,
Expand Down
42 changes: 40 additions & 2 deletions circuit_knitting/utils/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
:toctree: ../stubs

separate_circuit

.. autosummary::
:toctree: ../stubs
:template: autosummary/class_no_inherited_members.rst

SeparatedCircuits
"""
from __future__ import annotations
Expand All @@ -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]]
Expand All @@ -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
Copy link
Member Author

Choose a reason for hiding this comment

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

Without the _ = , one gets extra output from the non-None expressions:

>>> from qiskit import QuantumCircuit
>>> qc = QuantumCircuit(4)
>>> qc.x(0)
<qiskit.circuit.instructionset.InstructionSet object at 0x7f710e9921c0>
>>> qc.cx(1, 2)
<qiskit.circuit.instructionset.InstructionSet object at 0x7f70da3873a0>
>>> qc.h(3)
<qiskit.circuit.instructionset.InstructionSet object at 0x7f70da3872b0>

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()
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ extras =
test
cplex
commands =
pytest test/ {posargs}
pytest circuit_knitting/ test/ {posargs}

[testenv:style]
extras =
Expand Down