Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
cosenal opened this issue May 8, 2024 · 4 comments · Fixed by #2404
Closed

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

cosenal opened this issue May 8, 2024 · 4 comments · Fixed by #2404
Assignees
Labels
bug Something isn't working good first issue Good for newcomers unitaryhack-bounty
Milestone

Comments

@cosenal
Copy link
Contributor

cosenal commented May 8, 2024

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.

@cosenal cosenal added bug Something isn't working good first issue Good for newcomers labels May 8, 2024
@cosenal cosenal changed the title Qiskit Quantum Fourier Transform (QFT) circuits error out during convertion Qiskit Quantum Fourier Transform (QFT) circuits error out during conversion May 17, 2024
@DotandLog
Copy link

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?

@cosenal
Copy link
Contributor Author

cosenal commented May 23, 2024

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.

NnguyenHTommy added a commit to NnguyenHTommy/mitiq that referenced this issue Jun 6, 2024
…ns.py to decompose a qiskit circuit to native gates if an error occured. A test with zne was added to test for this with the QFT gate.
@NnguyenHTommy
Copy link
Contributor

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

@cosenal cosenal added this to the v0.38.0 milestone Jun 11, 2024
@cosenal
Copy link
Contributor Author

cosenal commented Jun 11, 2024

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

cosenal pushed a commit that referenced this issue Jun 11, 2024
* Fixed issue #2354 where a fallback was addded to conversions.py to decompose a qiskit circuit to native gates if an error occured. A test with zne was added to test for this with the QFT gate.

* added myself to the bottom of the authors file

* Adjusted code to follow the comments that cosenal placed with previous pull request.
Moved the exception catch from the from_qiskit() to the from_qasm() function as that function is
more general and is called by from_qiskit().
Used a QasmException catch instead of Exception to make it more clear that Qasm has trouble converting
Adjusted code to follow the comments that cosenal placed with previous pull request.
Moved the exception catch from the from_qiskit() to the from_qasm() function as that function is
more general and is called by from_qiskit().
Used a QasmException catch instead of Exception to make it more clear that Qasm has trouble converting
different gates. Removed choosing a basis for the transpile and let qiskit decide what gates to use.
Added an additional test case to test the from_qasm() function.

* resolving second round of changes reviewed by cosenal! moved the exception catch to the from_qiskit() function from the from_qasm() function as from_qasm() can handle non-qiskit circuits. Switched the test case to test the from_qiskit() function in doing so. Also only decomposed rather than transpiling and decomposing as transpiling doesn't do anything.

* renaming and added updated docstrings according to cosenal suggestions
@cosenal cosenal linked a pull request Jun 11, 2024 that will close this issue
@cosenal cosenal closed this as completed Jun 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers unitaryhack-bounty
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants