From ad8ec70e0118a8a77a64effa0019eb73dcc39f48 Mon Sep 17 00:00:00 2001 From: Michael Broughton Date: Fri, 21 Jan 2022 12:54:47 -0800 Subject: [PATCH 1/5] Add metadata property for cirq_google devices. --- .../cirq_google/devices/known_devices_test.py | 19 +++++++++- .../devices/serializable_device.py | 19 ++++++++++ .../devices/serializable_device_test.py | 35 +++++++++++++++++++ .../cirq_google/devices/xmon_device.py | 17 +++++++++ .../cirq_google/devices/xmon_device_test.py | 29 +++++++++++++++ 5 files changed, 118 insertions(+), 1 deletion(-) diff --git a/cirq-google/cirq_google/devices/known_devices_test.py b/cirq-google/cirq_google/devices/known_devices_test.py index b4b2d3f5ef1..15d2044c82a 100644 --- a/cirq-google/cirq_google/devices/known_devices_test.py +++ b/cirq-google/cirq_google/devices/known_devices_test.py @@ -21,13 +21,14 @@ def test_foxtail_qubits(): - with cirq.testing.assert_deprecated('Foxtail', deadline='v0.15'): + with cirq.testing.assert_deprecated('Foxtail', deadline='v0.15', count=2): expected_qubits = [] for i in range(0, 2): for j in range(0, 11): expected_qubits.append(cirq.GridQubit(i, j)) assert set(expected_qubits) == cirq_google.Foxtail.qubits + assert len(cirq_google.Foxtail.metadata.qubit_pairs) == 31 def test_foxtail_device_proto(): @@ -490,6 +491,22 @@ def test_sycamore_devices(device): assert device.duration_of(sqrt_iswap) == cirq.Duration(nanos=32) +def test_sycamore_metadata(): + assert len(cirq_google.Sycamore.metadata.qubit_pairs) == 88 + assert len(cirq_google.Sycamore23.metadata.qubit_pairs) == 32 + assert cirq_google.Sycamore.metadata.gateset == cirq.Gateset( + cirq.ops.fsim_gate.FSimGate, + cirq.ops.swap_gates.ISwapPowGate, + cirq.ops.phased_x_gate.PhasedXPowGate, + cirq.ops.common_gates.XPowGate, + cirq.ops.common_gates.YPowGate, + cirq.ops.common_gates.ZPowGate, + cirq.ops.phased_x_z_gate.PhasedXZGate, + cirq.ops.measurement_gate.MeasurementGate, + cirq.ops.wait_gate.WaitGate, + ) + + def test_sycamore_circuitop_device(): circuitop_gateset = cirq_google.SerializableGateSet( gate_set_name='circuitop_gateset', diff --git a/cirq-google/cirq_google/devices/serializable_device.py b/cirq-google/cirq_google/devices/serializable_device.py index b37792e9501..7af0c4b3f8e 100644 --- a/cirq-google/cirq_google/devices/serializable_device.py +++ b/cirq-google/cirq_google/devices/serializable_device.py @@ -108,6 +108,25 @@ def __init__( """ self.qubits = qubits self.gate_definitions = gate_definitions + self._metadata = cirq.GridDeviceMetadata( + [ + (pair[0], pair[1]) + for gate_defs in gate_definitions.values() + for gate_def in gate_defs + if gate_def.number_of_qubits == 2 + for pair in gate_def.target_set + if len(pair) == 2 and pair[0] < pair[1] + ], + cirq.Gateset( + *[g for g in gate_definitions.keys() if isinstance(g, (cirq.Gate, type(cirq.Gate)))] + ), + None, + ) + + @property + def metadata(self) -> cirq.GridDeviceMetadata: + """Get metadata information for device.""" + return self._metadata def qubit_set(self) -> FrozenSet[cirq.Qid]: return frozenset(self.qubits) diff --git a/cirq-google/cirq_google/devices/serializable_device_test.py b/cirq-google/cirq_google/devices/serializable_device_test.py index 59125d5b35b..394f3792632 100644 --- a/cirq-google/cirq_google/devices/serializable_device_test.py +++ b/cirq-google/cirq_google/devices/serializable_device_test.py @@ -77,6 +77,41 @@ def test_repr_pretty(cycle, func): printer.text.assert_called_once_with(func(device)) +def test_metadata_correct(): + qubits = cirq.GridQubit.rect(2, 3, left=1, top=1) + device_proto = cgdk.create_device_proto_for_qubits( + qubits=qubits, + pairs=[ + (qubits[0], qubits[1]), + (qubits[0], qubits[3]), + (qubits[1], qubits[4]), + (qubits[4], qubits[5]), + ], + gate_sets=[cg.FSIM_GATESET], + ) + device = cgdk.SerializableDevice.from_proto(device_proto, gate_sets=[cg.FSIM_GATESET]) + assert device.metadata.qubit_pairs == frozenset( + { + (qubits[0], qubits[1]), + (qubits[0], qubits[3]), + (qubits[1], qubits[4]), + (qubits[4], qubits[5]), + } + ) + assert device.metadata.gateset == cirq.Gateset( + cirq.FSimGate, + cirq.ISwapPowGate, + cirq.CZPowGate, + cirq.PhasedXPowGate, + cirq.XPowGate, + cirq.YPowGate, + cirq.ZPowGate, + cirq.PhasedXZGate, + cirq.MeasurementGate, + cirq.WaitGate, + ) + + def test_gate_definition_equality(): def1 = cg.devices.serializable_device._GateDefinition( duration=cirq.Duration(picos=4), diff --git a/cirq-google/cirq_google/devices/xmon_device.py b/cirq-google/cirq_google/devices/xmon_device.py index 9802a5429c5..fb2866bb229 100644 --- a/cirq-google/cirq_google/devices/xmon_device.py +++ b/cirq-google/cirq_google/devices/xmon_device.py @@ -45,6 +45,23 @@ def __init__( self._exp_w_duration = cirq.Duration(exp_w_duration) self._exp_z_duration = cirq.Duration(exp_11_duration) self.qubits = frozenset(qubits) + self._metadata = cirq.GridDeviceMetadata( + [(q0, q1) for q0 in self.qubits for q1 in self.qubits if q0.is_adjacent(q1)], + cirq.Gateset( + cirq.CZPowGate, + cirq.XPowGate, + cirq.YPowGate, + cirq.PhasedXPowGate, + cirq.MeasurementGate, + cirq.ZPowGate, + ), + None, + ) + + @property + def metadata(self) -> cirq.GridDeviceMetadata: + """Return the metadata for this device""" + return self._metadata def qubit_set(self) -> FrozenSet[cirq.GridQubit]: return self.qubits diff --git a/cirq-google/cirq_google/devices/xmon_device_test.py b/cirq-google/cirq_google/devices/xmon_device_test.py index 5ee00b9d551..08ad26c0e50 100644 --- a/cirq-google/cirq_google/devices/xmon_device_test.py +++ b/cirq-google/cirq_google/devices/xmon_device_test.py @@ -44,6 +44,35 @@ def qubits(self): raise NotImplementedError() +@mock.patch.dict(os.environ, clear='CIRQ_TESTING') +def test_device_metadata(): + d = square_device(3, 3) + assert d.metadata.gateset == cirq.Gateset( + cirq.CZPowGate, + cirq.XPowGate, + cirq.YPowGate, + cirq.PhasedXPowGate, + cirq.MeasurementGate, + cirq.ZPowGate, + ) + assert d.metadata.qubit_pairs == frozenset( + { + (cirq.GridQubit(0, 0), cirq.GridQubit(0, 1)), + (cirq.GridQubit(0, 1), cirq.GridQubit(1, 1)), + (cirq.GridQubit(2, 0), cirq.GridQubit(2, 1)), + (cirq.GridQubit(0, 0), cirq.GridQubit(1, 0)), + (cirq.GridQubit(0, 2), cirq.GridQubit(1, 2)), + (cirq.GridQubit(1, 0), cirq.GridQubit(2, 0)), + (cirq.GridQubit(1, 0), cirq.GridQubit(1, 1)), + (cirq.GridQubit(1, 1), cirq.GridQubit(2, 1)), + (cirq.GridQubit(1, 1), cirq.GridQubit(1, 2)), + (cirq.GridQubit(0, 1), cirq.GridQubit(0, 2)), + (cirq.GridQubit(2, 1), cirq.GridQubit(2, 2)), + (cirq.GridQubit(1, 2), cirq.GridQubit(2, 2)), + } + ) + + @mock.patch.dict(os.environ, clear='CIRQ_TESTING') def test_init(): d = square_device(2, 2, holes=[cirq.GridQubit(1, 1)]) From a23af63ee019096b4637da93ccb1f4a2eae46121 Mon Sep 17 00:00:00 2001 From: Michael Broughton Date: Fri, 21 Jan 2022 13:23:07 -0800 Subject: [PATCH 2/5] remove module paths in test. --- .../cirq_google/devices/known_devices_test.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/cirq-google/cirq_google/devices/known_devices_test.py b/cirq-google/cirq_google/devices/known_devices_test.py index 15d2044c82a..9c932c46ea2 100644 --- a/cirq-google/cirq_google/devices/known_devices_test.py +++ b/cirq-google/cirq_google/devices/known_devices_test.py @@ -495,15 +495,15 @@ def test_sycamore_metadata(): assert len(cirq_google.Sycamore.metadata.qubit_pairs) == 88 assert len(cirq_google.Sycamore23.metadata.qubit_pairs) == 32 assert cirq_google.Sycamore.metadata.gateset == cirq.Gateset( - cirq.ops.fsim_gate.FSimGate, - cirq.ops.swap_gates.ISwapPowGate, - cirq.ops.phased_x_gate.PhasedXPowGate, - cirq.ops.common_gates.XPowGate, - cirq.ops.common_gates.YPowGate, - cirq.ops.common_gates.ZPowGate, - cirq.ops.phased_x_z_gate.PhasedXZGate, - cirq.ops.measurement_gate.MeasurementGate, - cirq.ops.wait_gate.WaitGate, + cirq.FSimGate, + cirq.ISwapPowGate, + cirq.PhasedXPowGate, + cirq.XPowGate, + cirq.YPowGate, + cirq.ZPowGate, + cirq.PhasedXZGate, + cirq.MeasurementGate, + cirq.WaitGate, ) From ce5ab641a1cb50c028f83036fa646c9b0ce3779c Mon Sep 17 00:00:00 2001 From: Michael Broughton Date: Fri, 21 Jan 2022 14:39:07 -0800 Subject: [PATCH 3/5] feedback from everyone. --- cirq-google/cirq_google/devices/serializable_device.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cirq-google/cirq_google/devices/serializable_device.py b/cirq-google/cirq_google/devices/serializable_device.py index 7af0c4b3f8e..8b92b3b3136 100644 --- a/cirq-google/cirq_google/devices/serializable_device.py +++ b/cirq-google/cirq_google/devices/serializable_device.py @@ -109,7 +109,7 @@ def __init__( self.qubits = qubits self.gate_definitions = gate_definitions self._metadata = cirq.GridDeviceMetadata( - [ + qubit_pairs=[ (pair[0], pair[1]) for gate_defs in gate_definitions.values() for gate_def in gate_defs @@ -117,10 +117,10 @@ def __init__( for pair in gate_def.target_set if len(pair) == 2 and pair[0] < pair[1] ], - cirq.Gateset( + supported_gates=cirq.Gateset( *[g for g in gate_definitions.keys() if isinstance(g, (cirq.Gate, type(cirq.Gate)))] ), - None, + gate_durations=None, ) @property From 9795466b4f153e09af9104d2d4b86b38c411ef82 Mon Sep 17 00:00:00 2001 From: Michael Broughton Date: Fri, 21 Jan 2022 14:53:02 -0800 Subject: [PATCH 4/5] Orion feedback. --- .../devices/serializable_device_test.py | 22 +++++++------------ 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/cirq-google/cirq_google/devices/serializable_device_test.py b/cirq-google/cirq_google/devices/serializable_device_test.py index 394f3792632..230f6d3f446 100644 --- a/cirq-google/cirq_google/devices/serializable_device_test.py +++ b/cirq-google/cirq_google/devices/serializable_device_test.py @@ -79,25 +79,19 @@ def test_repr_pretty(cycle, func): def test_metadata_correct(): qubits = cirq.GridQubit.rect(2, 3, left=1, top=1) + pairs = [ + (qubits[0], qubits[1]), + (qubits[0], qubits[3]), + (qubits[1], qubits[4]), + (qubits[4], qubits[5]), + ] device_proto = cgdk.create_device_proto_for_qubits( qubits=qubits, - pairs=[ - (qubits[0], qubits[1]), - (qubits[0], qubits[3]), - (qubits[1], qubits[4]), - (qubits[4], qubits[5]), - ], + pairs=pairs, gate_sets=[cg.FSIM_GATESET], ) device = cgdk.SerializableDevice.from_proto(device_proto, gate_sets=[cg.FSIM_GATESET]) - assert device.metadata.qubit_pairs == frozenset( - { - (qubits[0], qubits[1]), - (qubits[0], qubits[3]), - (qubits[1], qubits[4]), - (qubits[4], qubits[5]), - } - ) + assert device.metadata.qubit_pairs == frozenset(pairs) assert device.metadata.gateset == cirq.Gateset( cirq.FSimGate, cirq.ISwapPowGate, From 42e44bf79132905f4dac47894a0e6ab692a8080c Mon Sep 17 00:00:00 2001 From: Michael Broughton Date: Tue, 25 Jan 2022 15:10:22 -0800 Subject: [PATCH 5/5] cov check. --- dev_tools/requirements/deps/pytest.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/dev_tools/requirements/deps/pytest.txt b/dev_tools/requirements/deps/pytest.txt index 9c48432afcd..b46b0533c04 100644 --- a/dev_tools/requirements/deps/pytest.txt +++ b/dev_tools/requirements/deps/pytest.txt @@ -3,6 +3,7 @@ pytest pytest-asyncio pytest-cov +coverage<=6.2 # for parallel testing notebooks pytest-xdist~=2.2.0