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

Replace SwapPermutationReplacer with cirq.map_operations #5655

Merged
merged 6 commits into from
Jul 6, 2022
Merged
30 changes: 10 additions & 20 deletions cirq-core/cirq/contrib/quantum_volume/quantum_volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,24 +197,6 @@ def process_results(
return data


class SwapPermutationReplacer(cirq.PointOptimizer):
"""Replaces SwapPermutationGates with their underlying implementation
gate."""

def __init__(self):
super().__init__()

def optimization_at(
self, circuit: cirq.Circuit, index: int, op: cirq.Operation
) -> Optional[cirq.PointOptimizationSummary]:
if isinstance(op.gate, cirq.contrib.acquaintance.SwapPermutationGate):
new_ops = op.gate.swap_gate.on(*op.qubits)
return cirq.PointOptimizationSummary(
clear_span=1, clear_qubits=op.qubits, new_operations=new_ops
)
return None # Don't make changes to other gates.


def compile_circuit(
circuit: cirq.Circuit,
*,
Expand Down Expand Up @@ -296,7 +278,15 @@ def compile_circuit(
mapping = swap_networks[0].final_mapping()
# Replace the PermutationGates with regular gates, so we don't proliferate
# the routing implementation details to the compiler and the device itself.
SwapPermutationReplacer().optimize_circuit(routed_circuit)

def replace_swap_permutation_gate(op: 'cirq.Operation', _):
if isinstance(op.gate, cirq.contrib.acquaintance.SwapPermutationGate):
return [op.gate.swap_gate.on(*op.qubits)]
return op

routed_circuit = cirq.map_operations_and_unroll(
routed_circuit, map_func=replace_swap_permutation_gate
)

if not compiler:
return CompilationResult(circuit=routed_circuit, mapping=mapping, parity_map=parity_map)
Expand All @@ -307,7 +297,7 @@ def compile_circuit(
# as well, we allow this to be passed in. This compiler is not allowed to
# change the order of the qubits.
return CompilationResult(
circuit=compiler(swap_networks[0].circuit), mapping=mapping, parity_map=parity_map
Copy link
Collaborator

@vtomole vtomole Jul 6, 2022

Choose a reason for hiding this comment

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

Why was swap_networks[0].circuit being passed in in the first place instead of routed_circuit?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Because the original optimizer mutated the circuit. routed_circuit was a variable storing a reference to swap_networks[0].circuit and since the underlying circuit was being mutated; it didn't matter whether you pass swap_networks[0].circuit or routed_circuit.

Now, routed_circuit stores a modified copy of swap_networks[0].circuit and therefore we need to pass routed_circuit instead of swap_networks[0].circuit.

circuit=compiler(routed_circuit), mapping=mapping, parity_map=parity_map
)


Expand Down