unitaryfund/mitiq

Qiskit Quantum Fourier Transform (QFT) circuits error out during conversion

cosenal opened this issue · 4 comments

If a Qiskit input circuit is composed of a QFT subcircuit, it errors out once it's executed with error mitigation techniques.

Minimum non-working snippet (adapted from #558 to comply with Qiskit 1.0):

from qiskit_aer import QasmSimulator
from qiskit import QuantumCircuit, compiler
from qiskit.circuit.library import QFT

from mitiq.interface.mitiq_qiskit import initialized_depolarizing_noise
from mitiq.zne import execute_with_zne


def qs_noisy_simulation(circuit: QuantumCircuit, shots: int = 1) -> float:

    noise_model = initialized_depolarizing_noise(noise_level=0.02)
    backend = QasmSimulator(noise_model=noise_model)
    job = backend.run(circuit.decompose(), shots=shots)
    return job.result().get_counts().get("0", 0.0) / shots


circuit = QuantumCircuit(1)
circuit &= QFT(1)
circuit.measure_all()

print(circuit)

unmitigated = qs_noisy_simulation(circuit)
print(unmitigated)

# circuit = compiler.transpile(circuit, basis_gates=["u1", "u2", "u3", "cx"])
# circuit = circuit.decompose()

mitigated = execute_with_zne(circuit, qs_noisy_simulation)

print(mitigated)

Error:

CircuitConversionError: Circuit could not be converted to an internal Mitiq circuit. This may be because the circuit contains custom gates or Pragmas (pyQuil). If you think this is a bug or that this circuit should be supported, you can open an issue at https://github.com/unitaryfund/mitiq. 

Traceback:

...
QasmException: Syntax error: 'q0'
..._QFT q0 { h q0; }
qreg q[1];
creg meas[1];
gate_QFT q[0];
measure q[0] -> meas[0]
        ^
at line 3, column 15
...

This is somehow different from #558 and #1073, which were raised on versions of Qiskit pre-1.0, and where the error was about unsupported gates.

One workaround is to either decompose or transpile the circuit before it's passed as an argument to quantum error mitigation routines (see commented lines in the snippet above)1. At the moment, this workaround is not explicit to the user, so we could implement a fallback mechanism in Mitiq interface, where a circuit gets decomposed or transpiled whenever the conversion to the internal representation fails.

This issue is only about Qiskit circuits, but in the future the mechanism implemented could be generalized to other frontends.

Footnotes

  1. note that decomposing the circuit is necessary also inside the executor for running the circuit on the simulator. However, that's not sufficient, because in error-mitigation routines (e.g. execute_with_zne) the conversion to Mitiq circuits happens before the body of the executor is run.

Hi @cosenal,

I recently learned about Mitiq and Qiskit, and this issue related appears to be an opportunity for me.

Can I contribute to this issue?

Hi @DotandLog – You're welcome to start looking at the issue now, but just so you are aware, this issue will most likely make it into the selection of Mitiq Unitary Hack bounties. If you wait a week, you can be eligible for that.

Hello!

I submitted a pull request and passed all the tests regarding this issue. This is my first open source contribution so I'm a bit new to how this works. Regarding documentation for this, I wasn't sure if anything needed to be updated since no new features were added

@NnguyenHTommy No documentation is needed, since this is more like a bug fix.