-
Notifications
You must be signed in to change notification settings - Fork 983
/
ion_device.py
163 lines (133 loc) · 5.91 KB
/
ion_device.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# 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.
from typing import Any, FrozenSet, Iterable, Optional, Set, TYPE_CHECKING
from cirq import circuits, value, devices, ops, protocols
from cirq.ion import convert_to_ion_gates
if TYPE_CHECKING:
import cirq
def get_ion_gateset() -> ops.Gateset:
return ops.Gateset(
ops.XXPowGate,
ops.MeasurementGate,
ops.XPowGate,
ops.YPowGate,
ops.ZPowGate,
ops.PhasedXPowGate,
unroll_circuit_op=False,
accept_global_phase_op=False,
)
@value.value_equality
class IonDevice(devices.Device):
"""A device with qubits placed on a line.
Qubits have all-to-all connectivity.
"""
def __init__(
self,
measurement_duration: 'cirq.DURATION_LIKE',
twoq_gates_duration: 'cirq.DURATION_LIKE',
oneq_gates_duration: 'cirq.DURATION_LIKE',
qubits: Iterable[devices.LineQubit],
) -> None:
"""Initializes the description of an ion trap device.
Args:
measurement_duration: The maximum duration of a measurement.
twoq_gates_duration: The maximum duration of a two qubit operation.
oneq_gates_duration: The maximum duration of a single qubit
operation.
qubits: Qubits on the device, identified by their x, y location.
"""
self._measurement_duration = value.Duration(measurement_duration)
self._twoq_gates_duration = value.Duration(twoq_gates_duration)
self._oneq_gates_duration = value.Duration(oneq_gates_duration)
self.qubits = frozenset(qubits)
self.gateset = get_ion_gateset()
def qubit_set(self) -> FrozenSet['cirq.LineQubit']:
return self.qubits
def qid_pairs(self) -> FrozenSet['cirq.SymmetricalQidPair']:
"""Qubits have all-to-all connectivity, so returns all pairs.
Returns:
All qubit pairs on the device.
"""
qs = self.qubits
return frozenset([devices.SymmetricalQidPair(q, q2) for q in qs for q2 in qs if q < q2])
def decompose_operation(self, operation: ops.Operation) -> ops.OP_TREE:
return convert_to_ion_gates.ConvertToIonGates().convert_one(operation)
def decompose_circuit(self, circuit: circuits.Circuit) -> circuits.Circuit:
return convert_to_ion_gates.ConvertToIonGates().convert_circuit(circuit)
def duration_of(self, operation):
if isinstance(operation.gate, ops.XXPowGate):
return self._twoq_gates_duration
if isinstance(
operation.gate, (ops.XPowGate, ops.YPowGate, ops.ZPowGate, ops.PhasedXPowGate)
):
return self._oneq_gates_duration
if isinstance(operation.gate, ops.MeasurementGate):
return self._measurement_duration
raise ValueError(f'Unsupported gate type: {operation!r}')
def validate_gate(self, gate: ops.Gate):
if gate not in self.gateset:
raise ValueError(f'Unsupported gate type: {gate!r}')
def validate_operation(self, operation):
if not isinstance(operation, ops.GateOperation):
raise ValueError(f'Unsupported operation: {operation!r}')
self.validate_gate(operation.gate)
for q in operation.qubits:
if not isinstance(q, devices.LineQubit):
raise ValueError(f'Unsupported qubit type: {q!r}')
if q not in self.qubits:
raise ValueError(f'Qubit not on device: {q!r}')
def validate_circuit(self, circuit: circuits.AbstractCircuit):
super().validate_circuit(circuit)
_verify_unique_measurement_keys(circuit.all_operations())
def at(self, position: int) -> Optional[devices.LineQubit]:
"""Returns the qubit at the given position, if there is one, else None."""
q = devices.LineQubit(position)
return q if q in self.qubits else None
def neighbors_of(self, qubit: devices.LineQubit) -> Iterable[devices.LineQubit]:
"""Returns the qubits that the given qubit can interact with."""
possibles = [
devices.LineQubit(qubit.x + 1),
devices.LineQubit(qubit.x - 1),
]
return [e for e in possibles if e in self.qubits]
def __repr__(self) -> str:
return (
f'IonDevice(measurement_duration={self._measurement_duration!r}, '
f'twoq_gates_duration={self._twoq_gates_duration!r}, '
f'oneq_gates_duration={self._oneq_gates_duration!r} '
f'qubits={sorted(self.qubits)!r})'
)
def __str__(self) -> str:
diagram = circuits.TextDiagramDrawer()
for q in self.qubits:
diagram.write(q.x, 0, str(q))
for q2 in self.neighbors_of(q):
diagram.grid_line(q.x, 0, q2.x, 0)
return diagram.render(horizontal_spacing=3, vertical_spacing=2, use_unicode_characters=True)
def _value_equality_values_(self) -> Any:
return (
self._measurement_duration,
self._twoq_gates_duration,
self._oneq_gates_duration,
self.qubits,
)
def _verify_unique_measurement_keys(operations: Iterable[ops.Operation]):
seen: Set[str] = set()
for op in operations:
if isinstance(op.gate, ops.MeasurementGate):
meas = op.gate
key = protocols.measurement_key_name(meas)
if key in seen:
raise ValueError(f'Measurement key {key} repeated')
seen.add(key)