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

pass down foreach #195

Merged
merged 3 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions bqskit/passes/control/foreach.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ class ForEachBlockPass(BasePass):
pass_down_key_prefix = 'ForEachBlockPass_pass_down_'
"""If a key exists in the pass data with this prefix, pass it to blocks."""

pass_down_block_specific_key_prefix = (
'ForEachBlockPass_specific_pass_down_'
)
"""Key for injecting a block specific pass data."""
mtweiden marked this conversation as resolved.
Show resolved Hide resolved

def __init__(
self,
loop_body: WorkflowLike,
Expand Down Expand Up @@ -197,6 +202,10 @@ async def run(self, circuit: Circuit, data: PassData) -> None:
for key in data:
if key.startswith(self.pass_down_key_prefix):
block_data[key] = data[key]
elif key.startswith(
self.pass_down_block_specific_key_prefix,
) and i in data[key]:
block_data[key] = data[key][i]
block_data.seed = data.seed

subcircuits.append(subcircuit)
Expand Down
33 changes: 33 additions & 0 deletions tests/passes/control/test_foreachblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@
from bqskit.compiler.basepass import BasePass
from bqskit.compiler.compiler import Compiler
from bqskit.compiler.passdata import PassData
from bqskit.compiler.workflow import Workflow
from bqskit.ir.circuit import Circuit
from bqskit.ir.gates import CircuitGate
from bqskit.ir.gates import CNOTGate
from bqskit.ir.gates import HGate
from bqskit.ir.gates import XGate
from bqskit.ir.gates import YGate
from bqskit.ir.gates import ZGate
from bqskit.ir.operation import Operation
from bqskit.passes import UnfoldPass
from bqskit.passes.control.foreach import ForEachBlockPass
from bqskit.passes.partitioning.quick import QuickPartitioner
from bqskit.passes.synthesis.qsearch import QSearchSynthesisPass
from bqskit.passes.util.update import UpdateDataPass


@pytest.fixture
Expand Down Expand Up @@ -130,3 +135,31 @@ def test_no_hang_on_empty_collection(compiler: Compiler) -> None:
circuit.append_gate(XGate(), 0)
feb_pass = ForEachBlockPass(RemoveXGatePass(), collection_filter=empty_coll)
compiler.compile(circuit, feb_pass)


def test_pass_down_seeds(compiler: Compiler) -> None:
circuit = Circuit(3)
circuit.append_gate(CNOTGate(), (0, 1))
circuit.append_gate(CNOTGate(), (1, 2))

seed = Circuit(3)
seed.append_gate(CNOTGate(), (0, 1))
seed.append_gate(CNOTGate(), (1, 2))

# Manually sed seed for block 0
mtweiden marked this conversation as resolved.
Show resolved Hide resolved
seeds = {0: [seed]}

key = 'ForEachBlockPass_specific_pass_down_seed_circuits'
mtweiden marked this conversation as resolved.
Show resolved Hide resolved

partitioner = QuickPartitioner()
updater = UpdateDataPass(key, seeds)
qsearch = QSearchSynthesisPass()
foreach = ForEachBlockPass(qsearch)
mtweiden marked this conversation as resolved.
Show resolved Hide resolved
unfolder = UnfoldPass()
workflow = Workflow([partitioner, updater, foreach, unfolder])
compiled, data = compiler.compile(
circuit, workflow, request_data=True,
)
dist = compiled.get_unitary().get_distance_from(circuit.get_unitary())
assert dist <= 1e-5
assert key in data
Loading