|
| 1 | +# Copyright 2022 The Cirq Developers |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""Tests for GridDevicemetadata.""" |
| 15 | + |
| 16 | +import pytest |
| 17 | +import cirq |
| 18 | +import networkx as nx |
| 19 | + |
| 20 | + |
| 21 | +def test_griddevice_metadata(): |
| 22 | + qubits = cirq.GridQubit.rect(2, 3) |
| 23 | + qubit_pairs = [(a, b) for a in qubits for b in qubits if a != b and a.is_adjacent(b)] |
| 24 | + |
| 25 | + gateset = cirq.Gateset(cirq.XPowGate, cirq.YPowGate, cirq.ZPowGate, cirq.CZ) |
| 26 | + metadata = cirq.GridDeviceMetadata(qubit_pairs, gateset) |
| 27 | + |
| 28 | + expected_pairings = frozenset( |
| 29 | + { |
| 30 | + (cirq.GridQubit(0, 0), cirq.GridQubit(0, 1)), |
| 31 | + (cirq.GridQubit(0, 1), cirq.GridQubit(0, 2)), |
| 32 | + (cirq.GridQubit(0, 1), cirq.GridQubit(1, 1)), |
| 33 | + (cirq.GridQubit(0, 2), cirq.GridQubit(1, 2)), |
| 34 | + (cirq.GridQubit(1, 0), cirq.GridQubit(1, 1)), |
| 35 | + (cirq.GridQubit(1, 1), cirq.GridQubit(1, 2)), |
| 36 | + (cirq.GridQubit(0, 0), cirq.GridQubit(1, 0)), |
| 37 | + } |
| 38 | + ) |
| 39 | + assert metadata.qubit_set == frozenset(qubits) |
| 40 | + assert metadata.qubit_pairs == expected_pairings |
| 41 | + assert metadata.gateset == gateset |
| 42 | + expected_graph = nx.Graph() |
| 43 | + expected_graph.add_edges_from(sorted(list(expected_pairings)), directed=False) |
| 44 | + assert metadata.nx_graph.edges() == expected_graph.edges() |
| 45 | + assert metadata.nx_graph.nodes() == expected_graph.nodes() |
| 46 | + assert metadata.gate_durations is None |
| 47 | + |
| 48 | + |
| 49 | +def test_griddevice_metadata_bad_durations(): |
| 50 | + qubits = tuple(cirq.GridQubit.rect(1, 2)) |
| 51 | + |
| 52 | + gateset = cirq.Gateset(cirq.XPowGate, cirq.YPowGate) |
| 53 | + invalid_duration = { |
| 54 | + cirq.Gateset(cirq.XPowGate): cirq.Duration(nanos=1), |
| 55 | + cirq.Gateset(cirq.ZPowGate): cirq.Duration(picos=1), |
| 56 | + } |
| 57 | + with pytest.raises(ValueError, match="ZPowGate"): |
| 58 | + cirq.GridDeviceMetadata([qubits], gateset, gate_durations=invalid_duration) |
| 59 | + |
| 60 | + |
| 61 | +def test_griddevice_json_load(): |
| 62 | + qubits = cirq.GridQubit.rect(2, 3) |
| 63 | + qubit_pairs = [(a, b) for a in qubits for b in qubits if a != b and a.is_adjacent(b)] |
| 64 | + gateset = cirq.Gateset(cirq.XPowGate, cirq.YPowGate, cirq.ZPowGate) |
| 65 | + duration = { |
| 66 | + cirq.Gateset(cirq.XPowGate): cirq.Duration(nanos=1), |
| 67 | + cirq.Gateset(cirq.YPowGate): cirq.Duration(picos=2), |
| 68 | + cirq.Gateset(cirq.ZPowGate): cirq.Duration(picos=3), |
| 69 | + } |
| 70 | + metadata = cirq.GridDeviceMetadata(qubit_pairs, gateset, gate_durations=duration) |
| 71 | + rep_str = cirq.to_json(metadata) |
| 72 | + assert metadata == cirq.read_json(json_text=rep_str) |
| 73 | + |
| 74 | + |
| 75 | +def test_griddevice_metadata_equality(): |
| 76 | + qubits = cirq.GridQubit.rect(2, 3) |
| 77 | + qubit_pairs = [(a, b) for a in qubits for b in qubits if a != b and a.is_adjacent(b)] |
| 78 | + gateset = cirq.Gateset(cirq.XPowGate, cirq.YPowGate, cirq.ZPowGate) |
| 79 | + duration = { |
| 80 | + cirq.Gateset(cirq.XPowGate): cirq.Duration(nanos=1), |
| 81 | + cirq.Gateset(cirq.YPowGate): cirq.Duration(picos=3), |
| 82 | + cirq.Gateset(cirq.ZPowGate): cirq.Duration(picos=2), |
| 83 | + } |
| 84 | + duration2 = { |
| 85 | + cirq.Gateset(cirq.XPowGate): cirq.Duration(nanos=10), |
| 86 | + cirq.Gateset(cirq.YPowGate): cirq.Duration(picos=13), |
| 87 | + cirq.Gateset(cirq.ZPowGate): cirq.Duration(picos=12), |
| 88 | + } |
| 89 | + metadata = cirq.GridDeviceMetadata(qubit_pairs, gateset, gate_durations=duration) |
| 90 | + metadata2 = cirq.GridDeviceMetadata(qubit_pairs[:2], gateset, gate_durations=duration) |
| 91 | + metadata3 = cirq.GridDeviceMetadata(qubit_pairs, gateset, gate_durations=None) |
| 92 | + metadata4 = cirq.GridDeviceMetadata(qubit_pairs, gateset, gate_durations=duration2) |
| 93 | + metadata5 = cirq.GridDeviceMetadata(reversed(qubit_pairs), gateset, gate_durations=duration) |
| 94 | + |
| 95 | + eq = cirq.testing.EqualsTester() |
| 96 | + eq.add_equality_group(metadata) |
| 97 | + eq.add_equality_group(metadata2) |
| 98 | + eq.add_equality_group(metadata3) |
| 99 | + eq.add_equality_group(metadata4) |
| 100 | + |
| 101 | + assert metadata == metadata5 |
| 102 | + |
| 103 | + |
| 104 | +def test_repr(): |
| 105 | + qubits = cirq.GridQubit.rect(2, 3) |
| 106 | + qubit_pairs = [(a, b) for a in qubits for b in qubits if a != b and a.is_adjacent(b)] |
| 107 | + gateset = cirq.Gateset(cirq.XPowGate, cirq.YPowGate, cirq.ZPowGate) |
| 108 | + duration = { |
| 109 | + cirq.Gateset(cirq.XPowGate): cirq.Duration(nanos=1), |
| 110 | + cirq.Gateset(cirq.YPowGate): cirq.Duration(picos=3), |
| 111 | + cirq.Gateset(cirq.ZPowGate): cirq.Duration(picos=2), |
| 112 | + } |
| 113 | + metadata = cirq.GridDeviceMetadata(qubit_pairs, gateset, gate_durations=duration) |
| 114 | + cirq.testing.assert_equivalent_repr(metadata) |
0 commit comments