BQSKit / bqskit

Berkeley Quantum Synthesis Toolkit
Other
114 stars 32 forks source link

Non-deterministic circuit iteration for regions #150

Closed mtweiden closed 1 year ago

mtweiden commented 1 year ago

When iterating through operations in a circuit region, I often observe that the ordering of operations in the iteration is not deterministic/does not correspond to their placements within a circuit.

Here's an example of a short program that produces different outputs for different runs:

` from typing import Sequence from bqskit import Circuit from bqskit.ir.gates import CNOTGate, U3Gate

def insert_unit(circuit : Circuit, location : Sequence[int]) -> Circuit: circuit.append_gate(CNOTGate(), location) circuit.append_gate(U3Gate(), [location[0]]) circuit.append_gate(U3Gate(), [location[1]]) return circuit

coupling_graph = [(0,1), (1,2), (2,3)] circuit = Circuit(4) circuit = insert_unit(circuit, [0,1]) circuit = insert_unit(circuit, [2,3]) circuit = insert_unit(circuit, [0,1]) circuit = insert_unit(circuit, [2,3]) circuit = insert_unit(circuit, [0,1]) circuit = insert_unit(circuit, [2,3])

for (a,b) in coupling_graph: region = {a:(0,circuit.depth), b:(0,circuit.depth)} print((a,b)) for op in circuit[region]: print(op) `

The last region produces the output: (2, 3) U3Gate@(3,) CNOTGate@(2, 3) CNOTGate@(2, 3) CNOTGate@(2, 3) U3Gate@(2,) U3Gate@(2,) U3Gate@(2,) U3Gate@(3,) U3Gate@(3,) Where it should instead produce: (2, 3) CNOTGate@(2, 3) U3Gate@(2,) U3Gate@(3,) CNOTGate@(2, 3) U3Gate@(2,) U3Gate@(3,) CNOTGate@(2, 3) U3Gate@(2,) U3Gate@(3,)

This is running on the dev-1.1 branch, on an Intel MacBook Pro with macOS Ventura.

dakk commented 1 year ago

I'm starting to work on it

dakk commented 1 year ago

Hi @mtweiden , I've submitted a PR last week, any change for a review before the UnitaryHack end?

mtweiden commented 1 year ago

Thanks for handling this!