1- # Copyright 2021 Q-CTRL
1+ # Copyright 2022 Q-CTRL
22#
33# Licensed under the Apache License, Version 2.0 (the "License");
44# you may not use this file except in compliance with the License.
3131FIX_DURATION_UNITARY = "fixed duration unitary"
3232INSTANT_UNITARY = "instant unitary"
3333
34+
3435def convert_dds_to_pyquil_program (
35- dynamic_decoupling_sequence ,
36- target_qubits = None ,
37- gate_time = 0.1 ,
38- add_measurement = True ,
39- algorithm = INSTANT_UNITARY ):
36+ dynamic_decoupling_sequence ,
37+ target_qubits = None ,
38+ gate_time = 0.1 ,
39+ add_measurement = True ,
40+ algorithm = INSTANT_UNITARY ,
41+ ):
4042
4143 """Converts a Dynamic Decoupling Sequence into quantum program
4244 as defined in pyQuil.
@@ -90,32 +92,41 @@ def convert_dds_to_pyquil_program(
9092 """
9193
9294 if dynamic_decoupling_sequence is None :
93- raise ArgumentsValueError ('No dynamic decoupling sequence provided.' ,
94- {'dynamic_decoupling_sequence' : dynamic_decoupling_sequence })
95+ raise ArgumentsValueError (
96+ "No dynamic decoupling sequence provided." ,
97+ {"dynamic_decoupling_sequence" : dynamic_decoupling_sequence },
98+ )
9599
96100 if not isinstance (dynamic_decoupling_sequence , DynamicDecouplingSequence ):
97- raise ArgumentsValueError ('Dynamical decoupling sequence is not recognized.'
98- 'Expected DynamicDecouplingSequence instance' ,
99- {'type(dynamic_decoupling_sequence)' :
100- type (dynamic_decoupling_sequence )})
101+ raise ArgumentsValueError (
102+ "Dynamical decoupling sequence is not recognized."
103+ "Expected DynamicDecouplingSequence instance" ,
104+ {"type(dynamic_decoupling_sequence)" : type (dynamic_decoupling_sequence )},
105+ )
101106
102107 target_qubits = target_qubits or [0 ]
103108
104109 if gate_time <= 0 :
105110 raise ArgumentsValueError (
106- 'Time delay of identity gate must be greater than zero.' ,
107- {'gate_time' : gate_time })
111+ "Time delay of identity gate must be greater than zero." ,
112+ {"gate_time" : gate_time },
113+ )
108114
109115 if np .any (target_qubits ) < 0 :
110116 raise ArgumentsValueError (
111- 'Every target qubits index must be non-negative.' ,
112- {'target_qubits' : target_qubits })
117+ "Every target qubits index must be non-negative." ,
118+ {"target_qubits" : target_qubits },
119+ )
113120
114121 if algorithm not in [FIX_DURATION_UNITARY , INSTANT_UNITARY ]:
115- raise ArgumentsValueError ('Algorithm must be one of {} or {}' .format (
116- INSTANT_UNITARY , FIX_DURATION_UNITARY ), {'algorithm' : algorithm })
122+ raise ArgumentsValueError (
123+ "Algorithm must be one of {} or {}" .format (
124+ INSTANT_UNITARY , FIX_DURATION_UNITARY
125+ ),
126+ {"algorithm" : algorithm },
127+ )
117128
118- unitary_time = 0.
129+ unitary_time = 0.0
119130 if algorithm == FIX_DURATION_UNITARY :
120131 unitary_time = gate_time
121132
@@ -127,11 +138,14 @@ def convert_dds_to_pyquil_program(
127138
128139 time_covered = 0
129140 program = Program ()
130- program += Pragma (' PRESERVE_BLOCK' )
141+ program += Pragma (" PRESERVE_BLOCK" )
131142
132143 for offset , rabi_rotation , azimuthal_angle , detuning_rotation in zip (
133- list (offsets ), list (rabi_rotations ),
134- list (azimuthal_angles ), list (detuning_rotations )):
144+ list (offsets ),
145+ list (rabi_rotations ),
146+ list (azimuthal_angles ),
147+ list (detuning_rotations ),
148+ ):
135149
136150 offset_distance = offset - time_covered
137151
@@ -143,10 +157,13 @@ def convert_dds_to_pyquil_program(
143157 "Offsets cannot be placed properly. Spacing between the rotations"
144158 "is smaller than the time required to perform the rotation. Provide"
145159 "a longer dynamic decoupling sequence or shorted gate time." ,
146- {'dynamic_decoupling_sequence' : dynamic_decoupling_sequence ,
147- 'gate_time' : gate_time })
160+ {
161+ "dynamic_decoupling_sequence" : dynamic_decoupling_sequence ,
162+ "gate_time" : gate_time ,
163+ },
164+ )
148165
149- while (time_covered + gate_time ) <= offset :
166+ while (time_covered + gate_time ) <= offset :
150167 for qubit in target_qubits :
151168 program += I (qubit )
152169 time_covered += gate_time
@@ -160,14 +177,16 @@ def convert_dds_to_pyquil_program(
160177 nonzero_pulse_counts = 3 - np .sum (zero_pulses )
161178 if nonzero_pulse_counts > 1 :
162179 raise ArgumentsValueError (
163- 'Open Controls support a sequence with one '
164- 'valid rotation at any offset. Found a sequence '
165- 'with multiple rotation operations at an offset.' ,
166- {'dynamic_decoupling_sequence' : dynamic_decoupling_sequence },
167- extras = {'offset' : offset ,
168- 'rabi_rotation' : rabi_rotation ,
169- 'azimuthal_angle' : azimuthal_angle ,
170- 'detuning_rotation' : detuning_rotation }
180+ "Open Controls support a sequence with one "
181+ "valid rotation at any offset. Found a sequence "
182+ "with multiple rotation operations at an offset." ,
183+ {"dynamic_decoupling_sequence" : dynamic_decoupling_sequence },
184+ extras = {
185+ "offset" : offset ,
186+ "rabi_rotation" : rabi_rotation ,
187+ "azimuthal_angle" : azimuthal_angle ,
188+ "detuning_rotation" : detuning_rotation ,
189+ },
171190 )
172191
173192 for qubit in target_qubits :
@@ -178,17 +197,16 @@ def convert_dds_to_pyquil_program(
178197 program += RX (rotations [0 ], qubit )
179198 elif not np .isclose (rotations [1 ], 0.0 ):
180199 program += RY (rotations [1 ], qubit )
181- elif not np .isclose (rotations [2 ], 0. ):
200+ elif not np .isclose (rotations [2 ], 0.0 ):
182201 program += RZ (rotations [2 ], qubit )
183202
184203 time_covered = offset + unitary_time
185204
186205 if add_measurement :
187- readout = program .declare ('ro' , ' BIT' , len (target_qubits ))
206+ readout = program .declare ("ro" , " BIT" , len (target_qubits ))
188207 for idx , qubit in enumerate (target_qubits ):
189208 program += MEASURE (qubit , readout [idx ])
190209
191- program += Pragma (' END_PRESERVE_BLOCK' )
210+ program += Pragma (" END_PRESERVE_BLOCK" )
192211
193212 return program
194-
0 commit comments