Commit 8e8e024
committed
extend qasm3 dumper with annotations in measurements
With this modification, qasm3 super extends annotations in measurements, which are only supported by mutable `Measure` subclass.
```python
from qiskit import qasm3
from qiskit.circuit import annotation, QuantumCircuit, Measure
class MidCircMeasurementAnnotation(annotation.Annotation):
namespace = "qcf.mid_circ_measure"
class MidCircuitMeasure(Measure):
"""A custom specialized measurement."""
def __init__(self, annotation_str=None):
super().__init__(label=annotation_str)
a = MidCircMeasurementAnnotation()
a.namespace += "." + str(annotation_str)
self.annotations = [a]
```
The annotation needs a serializers, but it is not used because, as defined above, does not have parameters:
```python
class Serializer(annotation.OpenQASM3Serializer):
def dump(self, annotation):
pass
def load(self, namespace, payload):
pass
```
It can be used in this way:
```python
measure_2 = MidCircuitMeasure("measure_2")
measure_3 = MidCircuitMeasure("measure_3")
circ = QuantumCircuit(2, 2)
circ.append(measure_2, [0], [0])
circ.append(measure_3, [1], [0])
circ.append(measure_2, [0], [1])
circ.measure_all()
handlers = {"qcf": Serializer()}
print(qasm3.dumps(circ, annotation_handlers=handlers))
```
```C
OPENQASM 3.0;
include "stdgates.inc";
bit[2] c;
bit[2] meas;
qubit[2] q;
@qcf.mid_circ_measure.measure_2AAA
c[0] = measure q[0];
@qcf.mid_circ_measure.measure_3
c[0] = measure q[1];
@qcf.mid_circ_measure.measure_2AAA
c[1] = measure q[0];
barrier q[0], q[1];
meas[0] = measure q[0];
meas[1] = measure q[1];
```1 parent 29d8bb0 commit 8e8e024
3 files changed
+26
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
388 | 388 | | |
389 | 389 | | |
390 | 390 | | |
391 | | - | |
| 391 | + | |
392 | 392 | | |
393 | | - | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
394 | 399 | | |
395 | 400 | | |
| 401 | + | |
396 | 402 | | |
397 | 403 | | |
398 | 404 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1003 | 1003 | | |
1004 | 1004 | | |
1005 | 1005 | | |
1006 | | - | |
| 1006 | + | |
| 1007 | + | |
| 1008 | + | |
| 1009 | + | |
| 1010 | + | |
| 1011 | + | |
| 1012 | + | |
| 1013 | + | |
| 1014 | + | |
| 1015 | + | |
| 1016 | + | |
| 1017 | + | |
| 1018 | + | |
| 1019 | + | |
| 1020 | + | |
| 1021 | + | |
1007 | 1022 | | |
1008 | 1023 | | |
1009 | 1024 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
273 | 273 | | |
274 | 274 | | |
275 | 275 | | |
| 276 | + | |
| 277 | + | |
276 | 278 | | |
277 | 279 | | |
278 | 280 | | |
| |||
0 commit comments