@@ -31,13 +31,21 @@ def qubit_to_proto_id(q: cirq.Qid) -> str:
31
31
For `cirq.NamedQubit`s this id is the name.
32
32
33
33
For `cirq.LineQubit`s this is string of the `x` attribute.
34
+
35
+ For `cirq_google.Coupler`s, this id is `c_{qubit0}_{qubit1}` where
36
+ qubit0 and qubit1 are the ids for the two Qid in the Coupler.
34
37
"""
38
+ # Avoid circular import
39
+ from cirq_google .devices .coupler import Coupler
40
+
35
41
if isinstance (q , cirq .GridQubit ):
36
42
return f'{ q .row } _{ q .col } '
37
43
elif isinstance (q , cirq .NamedQubit ):
38
44
return q .name
39
45
elif isinstance (q , cirq .LineQubit ):
40
46
return f'{ q .x } '
47
+ elif isinstance (q , Coupler ):
48
+ return f'c_{ qubit_to_proto_id (q .qubit0 )} _{ qubit_to_proto_id (q .qubit1 )} '
41
49
else :
42
50
raise ValueError (f'Qubits of type { type (q )} do not support proto id' )
43
51
@@ -49,6 +57,15 @@ def qubit_from_proto_id(proto_id: str) -> cirq.Qid:
49
57
50
58
Proto IDs of the form {int} are parsed as LineQubits.
51
59
60
+ Proto IDs of the form c_{int}_{int} are parsed as Couplers
61
+ between two LineQubits.
62
+
63
+ Proto IDs of the form c_{int}_{int}_{int}_{int} are parsed as Couplers
64
+ between two GridQubit.
65
+
66
+ Proto IDs of the form c_{name}_{name} are parsed as Couplers
67
+ between two NamedQubits.
68
+
52
69
All other proto IDs are parsed as NamedQubits. Note that this will happily
53
70
accept any string; for circuits which explicitly use Grid or LineQubits,
54
71
prefer one of the specialized methods below.
@@ -59,8 +76,29 @@ def qubit_from_proto_id(proto_id: str) -> cirq.Qid:
59
76
Returns:
60
77
A `cirq.Qid` corresponding to the proto id.
61
78
"""
62
- num_coords = len (proto_id .split ('_' ))
63
- if num_coords == 2 :
79
+ # Avoid circular import
80
+ from cirq_google .devices .coupler import Coupler
81
+
82
+ qubit_field = proto_id .split ('_' )
83
+ num_coords = len (qubit_field )
84
+ if proto_id [:2 ] == 'c_' :
85
+ if num_coords == 5 :
86
+ # 2 grid qubits: c_2_1_4_3
87
+ grid_qubit0_str = qubit_field [1 ] + '_' + qubit_field [2 ]
88
+ grid_qubit1_str = qubit_field [3 ] + '_' + qubit_field [4 ]
89
+ try :
90
+ grid_qubit0 = grid_qubit_from_proto_id (grid_qubit0_str )
91
+ grid_qubit1 = grid_qubit_from_proto_id (grid_qubit1_str )
92
+ return Coupler (grid_qubit0 , grid_qubit1 )
93
+ except ValueError :
94
+ pass # Not valid grid qubits.
95
+ elif num_coords == 3 :
96
+ # 2 line qubits: c_2_4
97
+ # Or two named qubits: c_qubita_qubitb
98
+ line_qubit0 = qubit_from_proto_id (qubit_field [1 ])
99
+ line_qubit1 = qubit_from_proto_id (qubit_field [2 ])
100
+ return Coupler (line_qubit0 , line_qubit1 )
101
+ elif num_coords == 2 :
64
102
try :
65
103
grid_q = grid_qubit_from_proto_id (proto_id )
66
104
return grid_q
0 commit comments