1313# limitations under the License.
1414
1515"""
16- ======================
1716qiskit.quantum_circuit
18- ======================
1917"""
2018
2119import numpy as np
22-
20+ from qctrlopencontrols import DynamicDecouplingSequence
21+ from qctrlopencontrols .exceptions import ArgumentsValueError
2322from qiskit import (
24- QuantumRegister , ClassicalRegister , QuantumCircuit )
23+ ClassicalRegister ,
24+ QuantumCircuit ,
25+ QuantumRegister ,
26+ )
2527from qiskit .qasm import pi
2628
27- from qctrlopencontrols import DynamicDecouplingSequence
28-
29- from qctrlopencontrols .globals import (FIX_DURATION_UNITARY , INSTANT_UNITARY )
30- from qctrlopencontrols .exceptions .exceptions import ArgumentsValueError
29+ FIX_DURATION_UNITARY = "fixed duration unitary"
30+ INSTANT_UNITARY = "instant unitary"
3131
3232
3333def convert_dds_to_qiskit_quantum_circuit (
34- dynamic_decoupling_sequence ,
35- target_qubits = None ,
36- gate_time = 0.1 ,
37- add_measurement = True ,
38- algorithm = INSTANT_UNITARY ,
39- quantum_registers = None ,
40- circuit_name = None ):
34+ dynamic_decoupling_sequence ,
35+ target_qubits = None ,
36+ gate_time = 0.1 ,
37+ add_measurement = True ,
38+ algorithm = INSTANT_UNITARY ,
39+ quantum_registers = None ,
40+ circuit_name = None ,
41+ ):
4142 """Converts a Dynamic Decoupling Sequence into QuantumCircuit
4243 as defined in Qiskit
4344
@@ -99,39 +100,50 @@ def convert_dds_to_qiskit_quantum_circuit(
99100 """
100101
101102 if dynamic_decoupling_sequence is None :
102- raise ArgumentsValueError ('No dynamic decoupling sequence provided.' ,
103- {'dynamic_decoupling_sequence' : dynamic_decoupling_sequence })
103+ raise ArgumentsValueError (
104+ "No dynamic decoupling sequence provided." ,
105+ {"dynamic_decoupling_sequence" : dynamic_decoupling_sequence },
106+ )
104107
105108 if not isinstance (dynamic_decoupling_sequence , DynamicDecouplingSequence ):
106- raise ArgumentsValueError ('Dynamical decoupling sequence is not recognized.'
107- 'Expected DynamicDecouplingSequence instance' ,
108- {'type(dynamic_decoupling_sequence)' :
109- type (dynamic_decoupling_sequence )})
109+ raise ArgumentsValueError (
110+ "Dynamical decoupling sequence is not recognized."
111+ "Expected DynamicDecouplingSequence instance" ,
112+ {"type(dynamic_decoupling_sequence)" : type (dynamic_decoupling_sequence )},
113+ )
110114
111115 target_qubits = target_qubits or [0 ]
112116
113117 if gate_time <= 0 :
114118 raise ArgumentsValueError (
115- 'Time delay of identity gate must be greater than zero.' ,
116- {'gate_time' : gate_time })
119+ "Time delay of identity gate must be greater than zero." ,
120+ {"gate_time" : gate_time },
121+ )
117122
118123 if np .any (target_qubits ) < 0 :
119124 raise ArgumentsValueError (
120- 'Every target qubits index must be non-negative.' ,
121- {'target_qubits' : target_qubits })
125+ "Every target qubits index must be non-negative." ,
126+ {"target_qubits" : target_qubits },
127+ )
122128
123129 if algorithm not in [FIX_DURATION_UNITARY , INSTANT_UNITARY ]:
124- raise ArgumentsValueError ('Algorithm must be one of {} or {}' .format (
125- INSTANT_UNITARY , FIX_DURATION_UNITARY ), {'algorithm' : algorithm })
130+ raise ArgumentsValueError (
131+ f"Algorithm must be one of { INSTANT_UNITARY } or { FIX_DURATION_UNITARY } " ,
132+ {"algorithm" : algorithm },
133+ )
126134
127135 if quantum_registers is not None :
128- if (max (target_qubits )+ 1 ) > len (quantum_registers ):
129- raise ArgumentsValueError ('Target qubit is not present in quantum_registers' ,
130- {'target_qubits' : target_qubits ,
131- 'size(quantum_registers)' : len (quantum_registers )},
132- extras = {'max(target_qubits)' : max (target_qubits )})
136+ if (max (target_qubits ) + 1 ) > len (quantum_registers ):
137+ raise ArgumentsValueError (
138+ "Target qubit is not present in quantum_registers" ,
139+ {
140+ "target_qubits" : target_qubits ,
141+ "size(quantum_registers)" : len (quantum_registers ),
142+ },
143+ extras = {"max(target_qubits)" : max (target_qubits )},
144+ )
133145 else :
134- quantum_registers = QuantumRegister (max (target_qubits )+ 1 )
146+ quantum_registers = QuantumRegister (max (target_qubits ) + 1 )
135147
136148 classical_registers = None
137149 if add_measurement :
@@ -143,7 +155,7 @@ def convert_dds_to_qiskit_quantum_circuit(
143155 if circuit_name is not None :
144156 quantum_circuit .name = circuit_name
145157
146- unitary_time = 0.
158+ unitary_time = 0.0
147159 if algorithm == FIX_DURATION_UNITARY :
148160 unitary_time = gate_time
149161
@@ -155,8 +167,11 @@ def convert_dds_to_qiskit_quantum_circuit(
155167
156168 time_covered = 0
157169 for offset , rabi_rotation , azimuthal_angle , detuning_rotation in zip (
158- list (offsets ), list (rabi_rotations ),
159- list (azimuthal_angles ), list (detuning_rotations )):
170+ list (offsets ),
171+ list (rabi_rotations ),
172+ list (azimuthal_angles ),
173+ list (detuning_rotations ),
174+ ):
160175
161176 offset_distance = offset - time_covered
162177
@@ -168,13 +183,20 @@ def convert_dds_to_qiskit_quantum_circuit(
168183 "Offsets cannot be placed properly. Spacing between the rotations"
169184 "is smaller than the time required to perform the rotation. Provide"
170185 "a longer dynamic decoupling sequence or shorted gate time." ,
171- {'dynamic_decoupling_sequence' : dynamic_decoupling_sequence ,
172- 'gate_time' : gate_time })
186+ {
187+ "dynamic_decoupling_sequence" : dynamic_decoupling_sequence ,
188+ "gate_time" : gate_time ,
189+ },
190+ )
173191
174- while (time_covered + gate_time ) <= offset :
192+ while (time_covered + gate_time ) <= offset :
175193 for qubit in target_qubits :
176- quantum_circuit .iden (quantum_registers [qubit ]) # pylint: disable=no-member
177- quantum_circuit .barrier (quantum_registers [qubit ]) # pylint: disable=no-member
194+ quantum_circuit .iden ( # pylint: disable=no-member
195+ quantum_registers [qubit ]
196+ )
197+ quantum_circuit .barrier ( # pylint: disable=no-member
198+ quantum_registers [qubit ]
199+ )
178200 time_covered += gate_time
179201
180202 x_rotation = rabi_rotation * np .cos (azimuthal_angle )
@@ -186,41 +208,53 @@ def convert_dds_to_qiskit_quantum_circuit(
186208 nonzero_pulse_counts = 3 - np .sum (zero_pulses )
187209 if nonzero_pulse_counts > 1 :
188210 raise ArgumentsValueError (
189- 'Open Controls support a sequence with one '
190- 'valid rotation at any offset. Found a sequence '
191- 'with multiple rotation operations at an offset.' ,
192- {'dynamic_decoupling_sequence' : dynamic_decoupling_sequence },
193- extras = {'offset' : offset ,
194- 'rabi_rotation' : rabi_rotation ,
195- 'azimuthal_angle' : azimuthal_angle ,
196- 'detuning_rotation' : detuning_rotation }
211+ "Open Controls support a sequence with one "
212+ "valid rotation at any offset. Found a sequence "
213+ "with multiple rotation operations at an offset." ,
214+ {"dynamic_decoupling_sequence" : dynamic_decoupling_sequence },
215+ extras = {
216+ "offset" : offset ,
217+ "rabi_rotation" : rabi_rotation ,
218+ "azimuthal_angle" : azimuthal_angle ,
219+ "detuning_rotation" : detuning_rotation ,
220+ },
197221 )
198222
199223 for qubit in target_qubits :
200224 if nonzero_pulse_counts == 0 :
201225 quantum_circuit .u3 (
202- 0. , 0. , 0. , # pylint: disable=no-member
203- quantum_registers [ qubit ] )
226+ 0.0 , 0.0 , 0.0 , quantum_registers [ qubit ] # pylint: disable=no-member
227+ )
204228 else :
205229 if not np .isclose (rotations [0 ], 0.0 ):
206230 quantum_circuit .u3 (
207- rotations [0 ], - pi / 2 , pi / 2 , # pylint: disable=no-member
208- quantum_registers [qubit ])
231+ rotations [0 ],
232+ - pi / 2 ,
233+ pi / 2 , # pylint: disable=no-member
234+ quantum_registers [qubit ],
235+ )
209236 elif not np .isclose (rotations [1 ], 0.0 ):
210237 quantum_circuit .u3 (
211- rotations [1 ], 0. , 0. , # pylint: disable=no-member
212- quantum_registers [qubit ])
213- elif not np .isclose (rotations [2 ], 0. ):
238+ rotations [1 ],
239+ 0.0 ,
240+ 0.0 , # pylint: disable=no-member
241+ quantum_registers [qubit ],
242+ )
243+ elif not np .isclose (rotations [2 ], 0.0 ):
214244 quantum_circuit .u1 (
215245 rotations [2 ], # pylint: disable=no-member
216- quantum_registers [qubit ])
217- quantum_circuit .barrier (quantum_registers [qubit ]) # pylint: disable=no-member
246+ quantum_registers [qubit ],
247+ )
248+ quantum_circuit .barrier (
249+ quantum_registers [qubit ]
250+ ) # pylint: disable=no-member
218251
219252 time_covered = offset + unitary_time
220253
221254 if add_measurement :
222255 for q_index , qubit in enumerate (target_qubits ):
223- quantum_circuit .measure (quantum_registers [qubit ], #pylint: disable=no-member
224- classical_registers [q_index ])
256+ quantum_circuit .measure ( # pylint: disable=no-member
257+ quantum_registers [qubit ], classical_registers [q_index ]
258+ )
225259
226260 return quantum_circuit
0 commit comments