68
68
from bqskit .ir .gates .parameterized .u1q import U1qGate
69
69
from bqskit .ir .gates .parameterized .u2 import U2Gate
70
70
from bqskit .ir .gates .parameterized .u3 import U3Gate
71
+ from bqskit .ir .gates .reset import Reset
71
72
from bqskit .ir .lang .language import LangException
72
73
from bqskit .ir .lang .qasm2 .parser import parse
73
74
from bqskit .ir .location import CircuitLocation
@@ -169,7 +170,6 @@ def __init__(self) -> None:
169
170
self .classical_regs : list [ClassicalReg ] = []
170
171
self .gate_def_parsing_obj : Any = None
171
172
self .custom_gate_defs : dict [str , CustomGateDef ] = {}
172
- self .measurements : dict [int , tuple [str , int ]] = {}
173
173
self .fill_gate_defs ()
174
174
175
175
def get_circuit (self ) -> Circuit :
@@ -180,12 +180,6 @@ def get_circuit(self) -> Circuit:
180
180
circuit = Circuit (num_qubits )
181
181
circuit .extend (self .op_list )
182
182
183
- # Add measurements
184
- if len (self .measurements ) > 0 :
185
- cregs = cast (List [Tuple [str , int ]], self .classical_regs )
186
- mph = MeasurementPlaceholder (cregs , self .measurements )
187
- circuit .append_gate (mph , list (self .measurements .keys ()))
188
-
189
183
return circuit
190
184
191
185
def fill_gate_defs (self ) -> None :
@@ -297,13 +291,6 @@ def gate(self, tree: lark.Tree) -> None:
297
291
qlist = tree .children [- 1 ]
298
292
location = CircuitLocation (self .convert_qubit_ids_to_indices (qlist ))
299
293
300
- if any (q in self .measurements for q in location ):
301
- raise LangException (
302
- 'BQSKit currently does not support mid-circuit measurements.'
303
- ' Unable to apply a gate on the same qubit where a measurement'
304
- ' has been previously made.' ,
305
- )
306
-
307
294
# Parse gate object
308
295
gate_name = str (tree .children [0 ])
309
296
if gate_name in self .gate_defs :
@@ -591,10 +578,16 @@ def creg(self, tree: lark.Tree) -> None:
591
578
592
579
def measure (self , tree : lark .Tree ) -> None :
593
580
"""Measure statement node visitor."""
581
+ params : list [float ] = []
582
+ measurements : dict [int , tuple [str , int ]] = {}
594
583
qubit_childs = tree .children [0 ].children
595
584
class_childs = tree .children [1 ].children
596
585
qubit_reg_name = str (qubit_childs [0 ])
597
586
class_reg_name = str (class_childs [0 ])
587
+ cregs = cast (List [Tuple [str , int ]], self .classical_regs )
588
+ qlist = tree .children [0 ]
589
+ location = CircuitLocation (self .convert_qubit_ids_to_indices (qlist ))
590
+
598
591
if not any (r .name == qubit_reg_name for r in self .qubit_regs ):
599
592
raise LangException (
600
593
f'Measuring undefined qubit register: { qubit_reg_name } ' ,
@@ -605,7 +598,7 @@ def measure(self, tree: lark.Tree) -> None:
605
598
f'Measuring undefined classical register: { class_reg_name } ' ,
606
599
)
607
600
608
- if len (qubit_childs ) == 1 and len (class_childs ) == 1 :
601
+ if len (qubit_childs ) == 1 and len (class_childs ) == 1 : # for measure all
609
602
for name , size in self .qubit_regs :
610
603
if qubit_reg_name == name :
611
604
qubit_size = size
@@ -625,34 +618,42 @@ def measure(self, tree: lark.Tree) -> None:
625
618
if name == qubit_reg_name :
626
619
break
627
620
outer_idx += size
628
-
629
621
for i in range (qubit_size ):
630
- self .measurements [outer_idx + i ] = (class_reg_name , i )
622
+ measurements [outer_idx + i ] = (class_reg_name , i )
623
+ mph = MeasurementPlaceholder (cregs , measurements )
631
624
632
625
elif len (qubit_childs ) == 2 and len (class_childs ) == 2 :
626
+ # measure qubits to clbits
633
627
qubit_index = int (qubit_childs [1 ])
634
628
class_index = int (class_childs [1 ])
635
-
636
- # Convert qubit_index to global index
637
- outer_idx = 0
638
- for name , size in self .qubit_regs :
639
- if name == qubit_reg_name :
640
- qubit_index = outer_idx + qubit_index
641
- break
642
- outer_idx += size
643
-
644
- self .measurements [qubit_index ] = (class_reg_name , class_index )
629
+ measurements [qubit_index ] = (class_reg_name , class_index )
630
+ mph = MeasurementPlaceholder (cregs , measurements )
645
631
646
632
else :
647
633
raise LangException (
648
634
'Invalid measurement: either a single qubit is being measured '
649
635
'to a full classical register or a qubit register is being '
650
636
'measured to a single classical bit.' ,
651
637
)
638
+ op = Operation (mph , location , params )
639
+ self .op_list .append (op )
652
640
653
641
def reset (self , tree : lark .Tree ) -> None :
654
- """Reset statement node visitor."""
655
- raise LangException ('BQSKit currently does not support resets.' )
642
+ """Reset node visitor."""
643
+ params : list [float ] = []
644
+ qlist = tree .children [- 1 ]
645
+ if len (qlist .children ) == 2 :
646
+ location = CircuitLocation (self .convert_qubit_ids_to_indices (qlist ))
647
+ op = Operation (Reset (), location , params )
648
+ self .op_list .append (op )
649
+ else :
650
+ locations = [
651
+ CircuitLocation (i )
652
+ for i in range (self .qubit_regs [0 ][1 ])
653
+ ]
654
+ for location in locations :
655
+ op = Operation (Reset (), location , params )
656
+ self .op_list .append (op )
656
657
657
658
def convert_qubit_ids_to_indices (self , qlist : lark .Tree ) -> list [int ]:
658
659
if qlist .data == 'anylist' :
0 commit comments