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

Add automated cut-finding module #520

Merged
merged 164 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from 162 commits
Commits
Show all changes
164 commits
Select commit Hold shift + click to select a range
6c29aa0
set up new branch with LO circuit cut finder
ibrahim-shehzad Jan 8, 2024
8f79eb3
Update tutorial with bug fix pending
ibrahim-shehzad Jan 9, 2024
3e8c438
update tutorial with bug fix pending
ibrahim-shehzad Jan 9, 2024
e084033
fix bug and update notebook
ibrahim-shehzad Jan 9, 2024
aa280e8
update print method, Co-authored by: Edwin Pednault [email protected]
ibrahim-shehzad Jan 9, 2024
654ec7c
Update cut finding dir structure
caleb-johnson Jan 9, 2024
cb648eb
commit before pulling changes
ibrahim-shehzad Jan 9, 2024
33c4096
Add license blurbs
caleb-johnson Jan 9, 2024
4251c20
black errors
caleb-johnson Jan 9, 2024
10304cc
snapshot before pulling
ibrahim-shehzad Jan 9, 2024
354e3f4
commit before pulling
ibrahim-shehzad Jan 9, 2024
9a73c0d
clean up notebook with updated print method
ibrahim-shehzad Jan 9, 2024
b22a29a
clean up printed output in tutorial
ibrahim-shehzad Jan 9, 2024
95f024d
Add find_cuts tutorial
caleb-johnson Jan 9, 2024
1df2289
Merge branch 'LO-circuit-cuts-optimizer' of github.com:Qiskit-Extensi…
caleb-johnson Jan 9, 2024
9938566
Use dataclasses for settings objects
caleb-johnson Jan 9, 2024
8e30782
Update tutorial to integrate with CKT
caleb-johnson Jan 9, 2024
3e10f89
edit string output function
ibrahim-shehzad Jan 10, 2024
eabf648
Merge branch 'LO-circuit-cuts-optimizer' of https://github.com/Qiskit…
ibrahim-shehzad Jan 10, 2024
aee698e
clean up utils doc strings.
ibrahim-shehzad Jan 10, 2024
0a8d590
Simplifications in xform func
caleb-johnson Jan 10, 2024
74d984a
black
caleb-johnson Jan 10, 2024
8148475
update xform code to fix small bug
caleb-johnson Jan 10, 2024
5ca795c
minor simplification
caleb-johnson Jan 10, 2024
d56a199
edit doc strings
ibrahim-shehzad Jan 11, 2024
78e0691
edit field name in settings
ibrahim-shehzad Jan 11, 2024
19321a6
Introduce a CircuitElement tuple
caleb-johnson Jan 17, 2024
56eb8a3
Merge remote-tracking branch 'origin/LO-circuit-cuts-optimizer' into …
caleb-johnson Jan 17, 2024
3b8f16f
Remove remnants of other search algorithms.
ibrahim-shehzad Jan 22, 2024
5d7c852
Merge branch 'LO-circuit-cuts-optimizer' of https://github.com/Qiskit…
ibrahim-shehzad Jan 22, 2024
71d2c60
Fix cost lookup logic
caleb-johnson Jan 22, 2024
754e5d2
snapshot test notebook before pull
ibrahim-shehzad Jan 22, 2024
a67ba62
Merge branch 'LO-circuit-cuts-optimizer' of https://github.com/Qiskit…
ibrahim-shehzad Jan 22, 2024
8d6ee74
Add cut finder tests
ibrahim-shehzad Jan 23, 2024
1c87d3d
rebase conflict
caleb-johnson Jan 24, 2024
5957759
Add license blurbs
caleb-johnson Jan 9, 2024
b257039
black errors
caleb-johnson Jan 9, 2024
41861e5
clean up notebook with updated print method
ibrahim-shehzad Jan 9, 2024
99c5259
clean up printed output in tutorial
ibrahim-shehzad Jan 9, 2024
4460c53
edit string output function
ibrahim-shehzad Jan 10, 2024
3e88ed9
Add find_cuts tutorial
caleb-johnson Jan 9, 2024
e5a762a
Use dataclasses for settings objects
caleb-johnson Jan 9, 2024
3686726
Update tutorial to integrate with CKT
caleb-johnson Jan 9, 2024
9e49305
clean up utils doc strings.
ibrahim-shehzad Jan 10, 2024
cf7268b
Simplifications in xform func
caleb-johnson Jan 10, 2024
97f87c1
black
caleb-johnson Jan 10, 2024
5635897
update xform code to fix small bug
caleb-johnson Jan 10, 2024
8c01c37
minor simplification
caleb-johnson Jan 10, 2024
3a090c1
edit doc strings
ibrahim-shehzad Jan 11, 2024
20ce84d
edit field name in settings
ibrahim-shehzad Jan 11, 2024
0d9eef8
Remove remnants of other search algorithms.
ibrahim-shehzad Jan 22, 2024
a4d6eb2
Introduce a CircuitElement tuple
caleb-johnson Jan 17, 2024
de0580a
Fix cost lookup logic
caleb-johnson Jan 22, 2024
741ed42
Fix bugs with wire cutting
caleb-johnson Jan 24, 2024
54cda84
Fix bugs with wire cutting
caleb-johnson Jan 24, 2024
06cf869
Update cco tests
ibrahim-shehzad Jan 24, 2024
8481b82
Merge branch 'LO-circuit-cuts-optimizer' of https://github.com/Qiskit…
ibrahim-shehzad Jan 24, 2024
d377b5d
Update indices to params in gate dict
caleb-johnson Jan 24, 2024
949266e
re run tutorial for correct outputs
caleb-johnson Jan 24, 2024
4b75973
clean up notebook
caleb-johnson Jan 24, 2024
6ed4528
clean up tutorial
caleb-johnson Jan 24, 2024
63ac13a
clean up tutorial
caleb-johnson Jan 24, 2024
c746e59
clean up tutorial
caleb-johnson Jan 24, 2024
8cd851b
clean up tutorial and print statement
caleb-johnson Jan 24, 2024
33a48fb
Add Ed Pednault as author
pednault Jan 25, 2024
6be5b1d
Set gamma UP and LB both to gamma until bell pair cutting is supporte…
caleb-johnson Jan 25, 2024
bb803fa
Remove redundant CircuitElement class
caleb-johnson Jan 25, 2024
43840dd
Remove unnecessary init
caleb-johnson Jan 25, 2024
b185c0d
Add partial type hints for a few modules
ibrahim-shehzad Jan 26, 2024
210f581
Merge branch 'LO-circuit-cuts-optimizer' of https://github.com/Qiskit…
ibrahim-shehzad Jan 26, 2024
1953082
Remove unused code and add roundtrip tests.
ibrahim-shehzad Jan 27, 2024
119ce29
Update BFS test and remove unused code.
ibrahim-shehzad Jan 29, 2024
ff58250
Fix black errors, changes to tests pending
ibrahim-shehzad Jan 30, 2024
c78c9a6
Update test to match new circuit interfaces
ibrahim-shehzad Jan 30, 2024
551237d
Update circuit interface tests and some docstrings.
ibrahim-shehzad Jan 31, 2024
077d4b5
Update CCOtoQC func and associated test.
ibrahim-shehzad Jan 31, 2024
7b35453
Update BFS test and run style.
ibrahim-shehzad Jan 31, 2024
19c78f2
Finish making all tests consistent with new circuit interface
ibrahim-shehzad Feb 1, 2024
5e55d84
Add and update tests
ibrahim-shehzad Feb 5, 2024
a57bd91
Add and update tests
ibrahim-shehzad Feb 5, 2024
5a9c113
Add type hints, tests, update func names.
ibrahim-shehzad Feb 9, 2024
40fd700
add tests, fix some mypy errors.
ibrahim-shehzad Feb 14, 2024
5cdcb68
Correct mypy errors, fix bugs in tests.
ibrahim-shehzad Feb 20, 2024
dfaff48
Remove cast subscripting to make compatible with py38.
ibrahim-shehzad Feb 26, 2024
ea0b2b8
Make type annotations compatible with min version tests.
ibrahim-shehzad Feb 26, 2024
07e2818
Fix more py38 type compatibility issues.
ibrahim-shehzad Feb 26, 2024
e1129af
Fix type hints.
ibrahim-shehzad Feb 26, 2024
5de66b8
Get rid of camel casing in function names and fix linting errors in d…
ibrahim-shehzad Mar 1, 2024
ea9ba93
Fix remaining pylint errors.
ibrahim-shehzad Mar 1, 2024
d374c25
Generate black diff.
ibrahim-shehzad Mar 1, 2024
b8ccbe4
Use python3.10 for lint workflow
garrison Mar 1, 2024
29c66ef
Merge branch 'main' into LO-circuit-cuts-optimizer
ibrahim-shehzad Mar 1, 2024
9abe1a1
Merge branch 'LO-circuit-cuts-optimizer' of https://github.com/Qiskit…
ibrahim-shehzad Mar 1, 2024
4bbac31
Run style with updated version of black
ibrahim-shehzad Mar 1, 2024
7d3872b
Edit doc strings
ibrahim-shehzad Mar 6, 2024
d07c13a
Find cuts updates (#498)
caleb-johnson Mar 7, 2024
94d96ae
Change to namedtuples, redo tests and typing.
ibrahim-shehzad Mar 8, 2024
7d68b14
Merge branch 'LO-circuit-cuts-optimizer' of https://github.com/Qiskit…
ibrahim-shehzad Mar 8, 2024
95a4885
Fix sphinx errors, change actions to named tuples.
ibrahim-shehzad Mar 8, 2024
115fe05
lint
ibrahim-shehzad Mar 8, 2024
0929184
clean up notebook, cast statements.
ibrahim-shehzad Mar 10, 2024
65108d8
Clean up type hints
ibrahim-shehzad Mar 11, 2024
bfd9411
style
ibrahim-shehzad Mar 11, 2024
7ffeb41
Add tests, add qubit list to output.
ibrahim-shehzad Mar 14, 2024
3609d17
style
ibrahim-shehzad Mar 14, 2024
b10c959
Add tests
ibrahim-shehzad Mar 15, 2024
e71dc7e
Remove subscripting in cast statements
ibrahim-shehzad Mar 15, 2024
14803d0
Fix tests.
ibrahim-shehzad Mar 15, 2024
915052b
Fix docstring. Improve error message. Add test.
caleb-johnson Mar 18, 2024
5d38550
Update tutorial, add classes for constraints and settings
ibrahim-shehzad Mar 18, 2024
7f4815d
Merge branch 'LO-circuit-cuts-optimizer' of https://github.com/Qiskit…
ibrahim-shehzad Mar 18, 2024
5f1ec95
Fix test, edit doc string.
ibrahim-shehzad Mar 18, 2024
9589465
Correct error in tutorial
ibrahim-shehzad Mar 19, 2024
23ddea6
Correct tutorial output
ibrahim-shehzad Mar 19, 2024
c44f740
Remove num_QPUs, add tests.
ibrahim-shehzad Mar 19, 2024
aa6ed33
Edit doc string, fix type hint for backjumps.
ibrahim-shehzad Mar 21, 2024
25a9d06
Fix bug in indexing in find_cuts, and fix docstring
caleb-johnson Mar 23, 2024
6493ac5
Fix funky rendering
caleb-johnson Mar 23, 2024
0c5d50f
docstring
caleb-johnson Mar 23, 2024
149e1f5
Fix remnant of indexing bug.
ibrahim-shehzad Mar 23, 2024
8337f64
Fix remnant of indexing bug.
ibrahim-shehzad Mar 23, 2024
0c2ceb5
Fix remnant of indexing bug.
ibrahim-shehzad Mar 23, 2024
3305225
Remove typo in cutting_optimization, update doc string.
ibrahim-shehzad Mar 23, 2024
22f99ee
Use new opt settings class
caleb-johnson Mar 23, 2024
ca597a4
Update to new class in cutting pkg
caleb-johnson Mar 23, 2024
de8a273
Fix style, docstring.
ibrahim-shehzad Mar 24, 2024
ad62b38
release note
caleb-johnson Mar 25, 2024
f09219f
Merge branch 'LO-circuit-cuts-optimizer' of github.com:Qiskit-Extensi…
caleb-johnson Mar 25, 2024
cc8788e
Edit release notes, clean up tutorial
ibrahim-shehzad Mar 25, 2024
1e497c4
Change to qubits per subcircuit everywhere
ibrahim-shehzad Mar 25, 2024
5a501a6
Expand release note
ibrahim-shehzad Mar 25, 2024
c832fc5
Create find_cuts module
caleb-johnson Mar 25, 2024
b53b84c
Merge branch 'main' of github.com:Qiskit-Extensions/circuit-knitting-…
caleb-johnson Mar 25, 2024
0cdf7df
Ignore CutBothWires for coverage
caleb-johnson Mar 25, 2024
2d9f884
Fix coverage
caleb-johnson Mar 26, 2024
363d901
black
caleb-johnson Mar 26, 2024
4f563d1
Clean up docstring
caleb-johnson Mar 26, 2024
a0a5b8e
Improve docstring
caleb-johnson Mar 26, 2024
62ad832
Upeate release notes
ibrahim-shehzad Mar 26, 2024
62a1121
Merge branch 'LO-circuit-cuts-optimizer' of https://github.com/Qiskit…
ibrahim-shehzad Mar 26, 2024
3cf4c9b
Remove extraneous tutorial
ibrahim-shehzad Mar 26, 2024
f91fac0
Add test, edit docstrings.
ibrahim-shehzad Mar 27, 2024
4b73535
Fix docstring typo
ibrahim-shehzad Mar 27, 2024
e1d6660
Docstring
ibrahim-shehzad Mar 27, 2024
f64ca8a
Import test circuit from qasm, edit docstrings and release notes.
ibrahim-shehzad Mar 28, 2024
68b9c1c
Correct path to test circuit
ibrahim-shehzad Mar 29, 2024
3558bb7
Edit docstring
ibrahim-shehzad Mar 29, 2024
d5b479d
Move find_cuts down into cut finding section of docs
caleb-johnson Mar 29, 2024
07da2bd
Update circuit_knitting/cutting/__init__.py
caleb-johnson Mar 29, 2024
9e7ada9
Construct test circuits in functions, not module (#521)
garrison Mar 29, 2024
cee023d
Change import routing, fix doc strings, add license blurbs.
ibrahim-shehzad Mar 29, 2024
639c8f0
Merge branch 'cut-finder' of https://github.com/Qiskit-Extensions/cir…
ibrahim-shehzad Mar 29, 2024
a1aa256
Move the cut-finder settings up into module.
caleb-johnson Mar 29, 2024
f671f18
Move settings for cut-finder up into cut-finding module
caleb-johnson Mar 29, 2024
f9bf314
black
caleb-johnson Mar 29, 2024
674e45b
coverage
caleb-johnson Mar 29, 2024
1487a3d
black
caleb-johnson Mar 29, 2024
3ae98ee
Update circuit_knitting/cutting/__init__.py
caleb-johnson Mar 29, 2024
744c7aa
Rename find_cuts module to automated_cut_finding
caleb-johnson Mar 29, 2024
72129a4
test import
caleb-johnson Mar 29, 2024
0d9fde7
cut_finding test imports
caleb-johnson Mar 29, 2024
c01adf7
Remove reference to OptimizationSettings in OptimizationParameters class
caleb-johnson Mar 29, 2024
002144e
Move tests, fix type hints and imports
ibrahim-shehzad Mar 29, 2024
16d6a76
Update import
ibrahim-shehzad Mar 29, 2024
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
4 changes: 2 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ jobs:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Python 3.9
- name: Set up Python 3.10
uses: actions/setup-python@v5
with:
python-version: '3.9'
python-version: '3.10'
- name: Install tox
run: |
python -m pip install --upgrade pip
Expand Down
21 changes: 21 additions & 0 deletions circuit_knitting/cutting/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@
instructions.CutWire
instructions.Move

Automatic Cut Finding
~~~~~~~~~~~~~~~~~~~~~

.. autosummary::
:toctree: ../stubs/
:nosignatures:

find_cuts
caleb-johnson marked this conversation as resolved.
Show resolved Hide resolved

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

automated_cut_finding.OptimizationParameters
automated_cut_finding.DeviceConstraints
ibrahim-shehzad marked this conversation as resolved.
Show resolved Hide resolved

Quasi-Probability Decomposition (QPD)
=====================================

Expand Down Expand Up @@ -85,6 +102,7 @@
from .cutting_experiments import generate_cutting_experiments
from .cutting_reconstruction import reconstruct_expectation_values
from .wire_cutting_transforms import cut_wires, expand_observables
from .automated_cut_finding import find_cuts, DeviceConstraints, OptimizationParameters

__all__ = [
"partition_circuit_qubits",
Expand All @@ -95,4 +113,7 @@
"PartitionedCuttingProblem",
"cut_wires",
"expand_observables",
"find_cuts",
"DeviceConstraints",
"OptimizationParameters",
]
157 changes: 157 additions & 0 deletions circuit_knitting/cutting/automated_cut_finding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
# This code is a Qiskit project.

# (C) Copyright IBM 2024.

# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

"""Function for automatically finding locations for gate and wire cuts."""

from __future__ import annotations

from typing import cast, Any
from dataclasses import dataclass

from qiskit.circuit import QuantumCircuit, CircuitInstruction

from .instructions import CutWire
from .cutting_decomposition import cut_gates
from .cut_finding.optimization_settings import OptimizationSettings
from .cut_finding.disjoint_subcircuits_state import DisjointSubcircuitsState
from .cut_finding.circuit_interface import SimpleGateList
from .cut_finding.lo_cuts_optimizer import LOCutsOptimizer
from .cut_finding.cco_utils import qc_to_cco_circuit


def find_cuts(
circuit: QuantumCircuit,
optimization: OptimizationParameters,
constraints: DeviceConstraints,
) -> tuple[QuantumCircuit, dict[str, float]]:
"""Find cut locations in a circuit, given optimization parameters and cutting constraints.

Args:
circuit: The circuit to cut. The input circuit may not contain gates acting
on more than two qubits.
optimization: Options for controlling optimizer behavior. Currently, the optimal
cuts are chosen using Dijkstra's best-first search algorithm.
constraints: Constraints on how the circuit may be partitioned
Returns:
A circuit containing :class:`.BaseQPDGate` instances. The subcircuits
resulting from cutting these gates will be runnable on the devices meeting
the ``constraints``.

A metadata dictionary:
- cuts: A list of length-2 tuples describing each cut in the output circuit.
The tuples are formatted as ``(cut_type: str, cut_id: int)``. The
cut ID is the index of the cut gate or wire in the output circuit's
``data`` field.
- sampling_overhead: The sampling overhead incurred from cutting the specified
gates and wires.

Raises:
ValueError: The input circuit contains a gate acting on more than 2 qubits.
"""
circuit_cco = qc_to_cco_circuit(circuit)
interface = SimpleGateList(circuit_cco)

opt_settings = OptimizationSettings(
seed=optimization.seed,
max_gamma=optimization.max_gamma,
max_backjumps=optimization.max_backjumps,
)

# Hard-code the optimizer to an LO-only optimizer
optimizer = LOCutsOptimizer(interface, opt_settings, constraints)

# Find cut locations
opt_out = optimizer.optimize()

wire_cut_actions = []
gate_ids = []

opt_out = cast(DisjointSubcircuitsState, opt_out)
opt_out.actions = cast(list, opt_out.actions)
for action in opt_out.actions:
if action.action.get_name() == "CutTwoQubitGate":
gate_ids.append(action.gate_spec.instruction_id)
else:
# The cut-finding optimizer currently only supports 4 cutting
# actions: {CutTwoQubitGate + these 3 wire cut types}
assert action.action.get_name() in (
"CutLeftWire",
"CutRightWire",
"CutBothWires",
)
wire_cut_actions.append(action)

# First, replace all gates to cut with BaseQPDGate instances.
# This assumes each gate to cut is replaced 1-to-1 with a QPD gate.
# This may not hold in the future as we stop treating gate cuts individually.
circ_out = cut_gates(circuit, gate_ids)[0]

# Insert all the wire cuts
counter = 0
for action in sorted(wire_cut_actions, key=lambda a: a[1][0]):
inst_id = action.gate_spec.instruction_id
# action.args[0][0] will be either 1 (control) or 2 (target)
qubit_id = action.args[0][0] - 1
circ_out.data.insert(
inst_id + counter,
CircuitInstruction(CutWire(), [circuit.data[inst_id].qubits[qubit_id]], []),
)
counter += 1

if action.action.get_name() == "CutBothWires": # pragma: no cover
# There should be two wires specified in the action in this case
assert len(action.args) == 2
qubit_id2 = action.args[1][0] - 1
circ_out.data.insert(
inst_id + counter,
CircuitInstruction(
CutWire(), [circuit.data[inst_id].qubits[qubit_id2]], []
),
)
counter += 1

# Return metadata describing the cut scheme
metadata: dict[str, Any] = {"cuts": []}
for i, inst in enumerate(circ_out.data):
if inst.operation.name == "qpd_2q":
metadata["cuts"].append(("Gate Cut", i))
elif inst.operation.name == "cut_wire":
metadata["cuts"].append(("Wire Cut", i))
metadata["sampling_overhead"] = opt_out.upper_bound_gamma() ** 2

return circ_out, metadata


@dataclass
class OptimizationParameters:
"""Specify parameters that control the optimization."""

seed: int | None = OptimizationSettings().seed
max_gamma: float = OptimizationSettings().max_gamma
max_backjumps: None | int = OptimizationSettings().max_backjumps


@dataclass
class DeviceConstraints:
"""Specify the constraints (qubits per subcircuit) that must be respected."""

qubits_per_subcircuit: int

def __post_init__(self):
"""Post-init method for data class."""
if self.qubits_per_subcircuit < 1:
raise ValueError(
"qubits_per_subcircuit must be a positive definite integer."
)

def get_qpu_width(self) -> int:
"""Return the number of qubits per subcircuit."""
return self.qubits_per_subcircuit
10 changes: 10 additions & 0 deletions circuit_knitting/cutting/cut_finding/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# (C) Copyright IBM 2024.

# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

"""Main automated cut finding functionality."""
Loading