Skip to content

Commit

Permalink
Merge pull request #222 from BQSKit/fix-identity-rebase
Browse files Browse the repository at this point in the history
Fix identity rebase
  • Loading branch information
edyounis authored Feb 28, 2024
2 parents 73ddab3 + ea5ca2d commit 80c072f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion bqskit/compiler/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,7 @@ def build_multi_qudit_retarget_workflow(
),
RestoreModelConnevtivityPass(),
],
AutoRebase2QuditGatePass(3, 5),
AutoRebase2QuditGatePass(3, 5, synthesis_epsilon),
),
ScanningGateRemovalPass(
success_threshold=synthesis_epsilon,
Expand Down
8 changes: 8 additions & 0 deletions bqskit/passes/retarget/auto.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from bqskit.ir.opt.cost.functions import HilbertSchmidtResidualsGenerator
from bqskit.ir.opt.cost.generator import CostFunctionGenerator
from bqskit.passes.retarget.two import Rebase2QuditGatePass
from bqskit.qis.unitary import UnitaryMatrix
from bqskit.runtime import get_runtime
from bqskit.utils.typing import is_integer
from bqskit.utils.typing import is_real_number
Expand Down Expand Up @@ -112,6 +113,13 @@ async def run(self, circuit: Circuit, data: PassData) -> None:

target = self.get_target(circuit, data)

if isinstance(target, UnitaryMatrix):
identity = UnitaryMatrix.identity(target.dim, target.radixes)
if target.get_distance_from(identity) < self.success_threshold:
_logger.debug('Target is identity, returning empty circuit.')
circuit.clear()
return

for g in old_gates:
# Track retries to check for no progress
num_retries = 0
Expand Down
9 changes: 9 additions & 0 deletions bqskit/passes/retarget/two.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from bqskit.ir.opt.cost.functions import HilbertSchmidtResidualsGenerator
from bqskit.ir.opt.cost.generator import CostFunctionGenerator
from bqskit.ir.point import CircuitPoint as Point
from bqskit.qis.unitary import UnitaryMatrix
from bqskit.runtime import get_runtime
from bqskit.utils.typing import is_integer
from bqskit.utils.typing import is_real_number
Expand Down Expand Up @@ -164,6 +165,13 @@ async def run(self, circuit: Circuit, data: PassData) -> None:

target = self.get_target(circuit, data)

if isinstance(target, UnitaryMatrix):
identity = UnitaryMatrix.identity(target.dim, target.radixes)
if target.get_distance_from(identity) < self.success_threshold:
_logger.debug('Target is identity, returning empty circuit.')
circuit.clear()
return

for g in self.gates:
# Track retries to check for no progress
num_retries = 0
Expand All @@ -188,6 +196,7 @@ async def run(self, circuit: Circuit, data: PassData) -> None:
circuit.point(g),
self.gates,
)

circuits_with_new_gate = []
for circ in self.circs:
circuit_copy = circuit.copy()
Expand Down
19 changes: 19 additions & 0 deletions tests/compiler/compile/test_corner_cases.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from __future__ import annotations

from bqskit.compiler.compile import compile
from bqskit.compiler.compiler import Compiler
from bqskit.ir.lang.qasm2 import OPENQASM2Language


def test_cry_identity_corner_case(compiler: Compiler) -> None:
qasm = """
OPENQASM 2.0;
include "qelib1.inc";
qreg q[5];
creg meas[5];
cry(0) q[0],q[2];
cry(0) q[1],q[2];
"""
circuit = OPENQASM2Language().decode(qasm)
_ = compile(circuit, optimization_level=1, seed=10)
# Should finish

0 comments on commit 80c072f

Please sign in to comment.