The file real_system.py inserts the measurement on a circuit after its transpilation.
However, the transpilation is done with the optimization_level=3 option, which means that the measurements plays an important role in the optimization.
For example, OptimizeSwapBeforeMeasure is a pass that removes the swap gates before the measurement, leading to a non-equivalent circuit (See the doc regarding this level 3: here).
In the line 39 of the current code, adding the measurement to qc1 after the transpilation prevents them from being used in the optimization and ultimately leads to a different circuit. See the different qasm of the generated code.
Original code:
The measurements are placed naively in order.
Note that the same problem happens with qc3 in the next lines.
How can we reproduce the issue?
I injected the alternative circuit in the code and I checked if the qasm codes are the same. Unfortunately it leads to different results.
You can use the code below:
...
qc1 = synth_qaoa1(a2, graph=graph, gamma=gamma, beta=beta)
# AS PER API AND QISKIT DOCUMENTATION
from copy import deepcopy
qc1_alternative = deepcopy(qc1)
qc1_alternative.measure_all()
# TRANSPILATION
qc1 = transpile(
qc1, basis_gates=['u3', 'cx'],
backend=backend, coupling_map=coup, optimization_level=3)
# ALTERNATIVE: MEASUREMENT BEFORE TRANSPILATION
qc1_alternative = transpile(
qc1_alternative, basis_gates=['u3', 'cx'],
backend=backend, coupling_map=coup, optimization_level=3)
# ORIGINAL
qc1.measure_all()
# check if the ORIGINAL and the ALTERNATIVE are the same
qasm_original = qc1.qasm()
qasm_alternative = qc1_alternative.qasm()
if qasm_original != qasm_alternative:
print("Warning: qasm codes are different")
print("Original:")
print(qasm_original)
print("Alternative:")
print(qasm_alternative)
...
What should happen?
The two qasm codes should be the same, thus the optimization should consider also the measurements.
Any suggestions?
The program can be fixed by moving the measurement before the transpilation. Following the recommendation of Qiskit. See here
Environment
Requirements from https://zenodo.org/record/5780204#.ZAdittLMKAk version 2
What is happening?
The file real_system.py inserts the measurement on a circuit after its transpilation. However, the transpilation is done with the
optimization_level=3
option, which means that the measurements plays an important role in the optimization. For example,OptimizeSwapBeforeMeasure
is a pass that removes the swap gates before the measurement, leading to a non-equivalent circuit (See the doc regarding this level 3: here). In the line 39 of the current code, adding the measurement toqc1
after the transpilation prevents them from being used in the optimization and ultimately leads to a different circuit. See the different qasm of the generated code.Original code: The measurements are placed naively in order.
Adding the measurement before transpilation: The measurement are optimized with level 3:
Note that the same problem happens with
qc3
in the next lines.How can we reproduce the issue?
I injected the alternative circuit in the code and I checked if the qasm codes are the same. Unfortunately it leads to different results. You can use the code below:
What should happen?
The two qasm codes should be the same, thus the optimization should consider also the measurements.
Any suggestions?
The program can be fixed by moving the measurement before the transpilation. Following the recommendation of Qiskit. See here