Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
0f4a4b5
[WIP] optimization playground
Strilanc Jul 12, 2018
f1073c0
Implement EjectFullW optimizer
Strilanc Jul 15, 2018
de00b46
doc
Strilanc Jul 15, 2018
cbdb073
Merge branch 'master' of github.com:quantumlib/cirq into optimization…
Strilanc Jul 15, 2018
01c98b2
r
Strilanc Jul 15, 2018
977b02b
Merge branch 'diff3' into optimization_cleanup
Strilanc Jul 15, 2018
ee07218
Define cirq.google.optimize_for_xmon
Strilanc Jul 15, 2018
fc727bc
Merge branch 'master' of github.com:quantumlib/cirq into optimization…
Strilanc Jul 17, 2018
0d0f5ae
Fix sign error in EjectFullW and silent merge error in eject_z_test.py
Strilanc Jul 17, 2018
c4dce77
Fix sign error in EjectFullW and silent merge error in eject_z_test.py
Strilanc Jul 17, 2018
4a46caf
condition
Strilanc Jul 17, 2018
36ed1e0
cover
Strilanc Jul 17, 2018
18c1f77
Merge branch 'master' of github.com:quantumlib/cirq into fix-w
Strilanc Jul 17, 2018
b25a890
Merge branch 'fix-w' into optimization_cleanup
Strilanc Jul 17, 2018
a6b0a74
fix
Strilanc Jul 17, 2018
08b7416
Merge branch 'fix-w' into optimization_cleanup
Strilanc Jul 17, 2018
c0bfb40
test_ccz
Strilanc Jul 17, 2018
72d9aee
Refactor drop_negligible into an OptimizationPass using batch_remove
Strilanc Jul 17, 2018
7cd58e6
Fix merge_interactions failing to convert into xmon gates
Strilanc Jul 17, 2018
e9c5acb
Merge branch 'master' of github.com:quantumlib/cirq into optimize-1
Strilanc Jul 17, 2018
185b517
fix tests
Strilanc Jul 17, 2018
07c1fde
Merge branch 'optimize-0' into optimization_cleanup
Strilanc Jul 17, 2018
094b3cb
Merge branch 'optimize-1' into optimization_cleanup
Strilanc Jul 17, 2018
3fca893
cut bool
Strilanc Jul 17, 2018
86dc1d0
naming things
Strilanc Jul 17, 2018
091aebe
Merge branch 'optimize-1' into optimization_cleanup
Strilanc Jul 17, 2018
27cadfb
Cleanup imports in merge_rotations_test
Strilanc Jul 17, 2018
6a64583
Merge branch 'master' of github.com:quantumlib/cirq into optimization…
Strilanc Jul 17, 2018
1febf3d
Add cirq.testing.assert_circuits_with_terminal_measurements_are_equiv…
Strilanc Jul 17, 2018
9de3e03
lint
Strilanc Jul 17, 2018
ff8a4fc
compare_unitaries
Strilanc Jul 17, 2018
dc26303
dindent
Strilanc Jul 17, 2018
c511210
Merge branch 'optimize-3' into optimization_cleanup
Strilanc Jul 17, 2018
d8aa6c5
Merge branch 'optimize-2' into optimization_cleanup
Strilanc Jul 17, 2018
75b9501
fix
Strilanc Jul 17, 2018
2fb7103
Fix
Strilanc Jul 17, 2018
37ef6ff
lint
Strilanc Jul 17, 2018
9007754
Merge branch 'master' of github.com:quantumlib/cirq into optimization…
Strilanc Jul 17, 2018
296bdea
Merge branch 'master' of github.com:quantumlib/cirq into optimization…
Strilanc Jul 18, 2018
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
6 changes: 3 additions & 3 deletions cirq/circuits/optimization_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,12 @@ def optimization_at(self,
pass

def optimize_circuit(self, circuit: Circuit):
walls = defaultdict(lambda: 0) # type: Dict[QubitId, int]
frontier = defaultdict(lambda: 0) # type: Dict[QubitId, int]
i = 0
while i < len(circuit): # Note: circuit may mutate as we go.
for op in circuit[i].operations:
# Don't touch stuff inserted by previous optimizations.
if any(walls[q] > i for q in op.qubits):
if any(frontier[q] > i for q in op.qubits):
continue

# Skip if an optimization removed the circuit underneath us.
Expand All @@ -138,7 +138,7 @@ def optimize_circuit(self, circuit: Circuit):
circuit.clear_operations_touching(
opt.clear_qubits,
[e for e in range(i, i + opt.clear_span)])
circuit.insert_at_frontier(opt.new_operations, i, walls)
circuit.insert_at_frontier(opt.new_operations, i, frontier)

i += 1

Expand Down
3 changes: 3 additions & 0 deletions cirq/google/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
from cirq.google.xmon_gate_extensions import (
xmon_gate_ext,
)
from cirq.google.optimize import (
optimized_for_xmon,
)
from cirq.google.xmon_gates import (
Exp11Gate,
ExpWGate,
Expand Down
58 changes: 58 additions & 0 deletions cirq/google/optimize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Copyright 2018 The Cirq Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""A combination of several optimizations targeting XmonDevice."""

from cirq import circuits
from cirq.google import (
convert_to_xmon_gates,
merge_rotations,
merge_interactions,
eject_full_w,
eject_z,
)

_TOLERANCE = 1e-5

_OPTIMIZERS = [
convert_to_xmon_gates.ConvertToXmonGates(),

merge_interactions.MergeInteractions(tolerance=_TOLERANCE),
merge_rotations.MergeRotations(tolerance=_TOLERANCE),
eject_full_w.EjectFullW(tolerance=_TOLERANCE),
eject_z.EjectZ(tolerance=_TOLERANCE),
circuits.DropNegligible(tolerance=_TOLERANCE),
]


def optimized_for_xmon(circuit: circuits.Circuit) -> circuits.Circuit:
"""Optimizes a circuit with XmonDevice in mind.

Starts by converting the circuit's operations to the xmon gate set, then
begins merging interactions and rotations, ejecting pi-rotations and phasing
operations, dropping unnecessary operations, and pushing operations earlier.

Args:
circuit: The circuit to optimize.

Returns:
The optimized circuit.
"""
copy = circuit.copy()
for optimizer in _OPTIMIZERS:
optimizer.optimize_circuit(copy)

return circuits.Circuit().from_ops(
copy.all_operations(),
strategy=circuits.InsertStrategy.EARLIEST)
58 changes: 58 additions & 0 deletions cirq/google/optimize_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Copyright 2018 The Cirq Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest

import cirq
import cirq.google as cg

from cirq.testing import (
assert_circuits_with_terminal_measurements_are_equivalent,
)


@pytest.mark.parametrize('n,d', [
(3, 2),
(4, 3),
(4, 4),
(5, 4),
(22, 4),
])
def test_swap_field(n: int, d: int):
before = cirq.Circuit.from_ops(
cirq.ISWAP(cirq.LineQubit(j), cirq.LineQubit(j + 1))
for i in range(d)
for j in range(i % 2, n - 1, 2)
)
before.append(cirq.measure(*before.all_qubits()))

after = cg.optimized_for_xmon(before)

assert len(after) == d*4 + 2
if n <= 5:
assert_circuits_with_terminal_measurements_are_equivalent(
before, after, atol=1e-4)


def test_ccz():
before = cirq.Circuit.from_ops(
cirq.CCZ(cirq.GridQubit(5, 5),
cirq.GridQubit(5, 6),
cirq.GridQubit(5, 7)))
Copy link
Collaborator

Choose a reason for hiding this comment

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

No terminal measurements? Would it be useful to have separate tests for both cases (with measurements and without)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Intentional.


after = cg.optimized_for_xmon(before)

assert len(after) <= 22
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is the optimized length currently 22?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. But I'm happy if it goes down, too.

assert_circuits_with_terminal_measurements_are_equivalent(
before, after, atol=1e-4)