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

Some Qiskit gates are not supported (during the internal conversion to Cirq) #1073

Closed
andreamari opened this issue Jan 6, 2022 · 19 comments · Fixed by #2585
Closed

Some Qiskit gates are not supported (during the internal conversion to Cirq) #1073

andreamari opened this issue Jan 6, 2022 · 19 comments · Fixed by #2585
Assignees
Labels
good first issue Good for newcomers interface-and-conversions How Mitiq interfaces with quantum software packages. priority/low A non-urgent issue to fix or idea to discuss.

Comments

@andreamari
Copy link
Member

andreamari commented Jan 6, 2022

Some Qiskit gates are not supported (during the internal conversion to Cirq).
It would be good to support more gates.
Since we are relying on Cirq conversion functions, a possible way to do achieve can be via up-streaming these requests to Cirq.

Gates that are currently not supported (please add more if you know more):

@andreamari andreamari added priority/low A non-urgent issue to fix or idea to discuss. interface-and-conversions How Mitiq interfaces with quantum software packages. feature-request A request for a feature, tool, or workflow in Mitiq. labels Jan 6, 2022
@rmlarose
Copy link
Contributor

rmlarose commented Jan 6, 2022

Logistically, I suggest closing #558 and using this issue to track to avoid duplicates.

@github-actions
Copy link

This issue had no activity for 2 months, and will be closed in one week unless there is new activity. Cheers!

@github-actions
Copy link

This issue had no activity for 4 months, and will be closed in 2 weeks unless there is new activity. Cheers!

@github-actions github-actions bot added the stale label Aug 26, 2022
@andreamari andreamari removed the stale label Aug 29, 2022
@natestemen natestemen added this to the 0.25.0 milestone Mar 2, 2023
@natestemen natestemen added the needs/scoping-or-clarification An issue that is too vague to act on & needs further clarification or scoping. label Mar 3, 2023
@natestemen
Copy link
Member

natestemen commented Mar 3, 2023

Scope of this ticket is twofold:

  1. Do some investigation to figure out if the gates mentioned above are still failing (and documenting the results)
  2. Adding support for failing gates

For milestone 0.25 lets complete point 1, and add support for failing gates that are straightforward to fix.

@natestemen natestemen assigned natestemen and unassigned natestemen Mar 3, 2023
@natestemen natestemen removed this from the 0.25.0 milestone Mar 3, 2023
@MaldoAlberto
Copy link

I can work on this issue

@natestemen natestemen removed feature-request A request for a feature, tool, or workflow in Mitiq. needs/scoping-or-clarification An issue that is too vague to act on & needs further clarification or scoping. labels Aug 22, 2023
@MaldoAlberto
Copy link

Hi @andreamari ! I'm working on this issue and I find Qiskit works now with OpenQASM3 and cirq with OpenQASM2, and using less gates than qiskit, so my question is convenient to do a function to converts OpenQASM3 to OpenQASM2 and include the equivalents gates, for example

cu(pi/2, pi/2, pi/2, 0) q[1], q[0];

will be

p(0) q[1];
p((pi/2 + pi/2) / 2) q[1];
p((pi/2 - pi/2) / 2) q[0];
cx q[1], q[0];
u(-pi/2 / 2, 0, -(pi/2 + pi/2) / 2) q[0];
cx q[1], q[0];
u(pi/2 / 2, pi/2, 0) q[0];

@andreamari
Copy link
Member Author

Hi @MaldoAlberto,

Thanks for isolating the root of problem so well!
I am not sure about writing from scratch a QASM3<->QASM2 within Mitiq. It would be a complex piece of code to maintain which could solve this bug, but could create 100 new bugs :-)

Two less dangerous solutions are perhaps the following:

  • Open some issues on Cirq or Qiskit about conversion problems. Having good Qiskit<-->Cirq conversions is in their interest and they may find good and stable solutions.
  • Replace non-supported gates with equivalent supported decompositions, before converting. This is similar to what you proposed, but instead of doing it at the QASM level, it could be done at the qiskit.QuantumCircuit level.

Note that the last step is not as easy as it seems, since the pipeline of conversion layers in mitiq.interface is a bit complicated (e.g. nested decorators).

@MaldoAlberto
Copy link

Hi @MaldoAlberto,

Thanks for isolating the root of problem so well! I am not sure about writing from scratch a QASM3<->QASM2 within Mitiq. It would be a complex piece of code to maintain which could solve this bug, but could create 100 new bugs :-)

Two less dangerous solutions are perhaps the following:

  • Open some issues on Cirq or Qiskit about conversion problems. Having good Qiskit<-->Cirq conversions is in their interest and they may find good and stable solutions.
  • Replace non-supported gates with equivalent supported decompositions, before converting. This is similar to what you proposed, but instead of doing it at the QASM level, it could be done at the qiskit.QuantumCircuit level.

Note that the last step is not as easy as it seems, since the pipeline of conversion layers in mitiq.interface is a bit complicated (e.g. nested decorators).

That's true! I can try to do both. Additionally, I'll examine the conversion in 'qiskit.Quantum' to resolve it as soon as possible. Thank you for your suggestions.

@cosenal cosenal added the monitor-for-external-solution Tracking issues caused by external packages label Apr 11, 2024
@cosenal
Copy link
Contributor

cosenal commented Apr 11, 2024

On hold until migration to Qiskit 1.0 is completed #2204 Qiskit 1.0 is now supported, this issue is ready to be worked on.

@cosenal cosenal removed the monitor-for-external-solution Tracking issues caused by external packages label Apr 21, 2024
@jordandsullivan
Copy link
Contributor

Let's check if the original statement of this issue is still valid for the new Qiskit 1.0 integration. If so, I suggest splitting the issue into separate ones for each gate.

@cosenal
Copy link
Contributor

cosenal commented May 6, 2024

The original statement, as well as the rest of the discussion, still hold for Qiskit 1.0. In order to reproduce the issue, one can run a small modification of the code snippet from #558 (the modifications were to make the snippet compatible with the latest qiskit packages):

import functools

from mitiq.interface.mitiq_qiskit import initialized_depolarizing_noise
from qiskit_aer import QasmSimulator
from qiskit import QuantumRegister, ClassicalRegister, transpile
from qiskit import QuantumCircuit
from qiskit.circuit.library import QFT
from mitiq.zne import execute_with_zne


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

    noise_model = initialized_depolarizing_noise(noise_level=0.02)
    backend = QasmSimulator(noise_model=noise_model)
    # Transpile the circuit so it can be properly run
    exec_circuit = transpile(
        circuit,
        backend=backend,
        basis_gates=noise_model.basis_gates if noise_model else None,
        optimization_level=0, # Important to preserve folded gates.
    )
    # Run the circuit
    job = backend.run(exec_circuit, shots=shots)

    return job.result().get_counts().get(correct_bitstring, 0.0) / shots

q = QuantumRegister(5,'q')
c = ClassicalRegister(5,'c')

circuit = QuantumCircuit(q,c)
circuit.x(q[0])
circuit.x(q[2])
circuit.x(q[4])
circuit &= QFT(num_qubits=5, approximation_degree=0, do_swaps=True, inverse=False, insert_barriers=True, name='qft')
circuit &= QFT(num_qubits=5, approximation_degree=0, do_swaps=True, inverse=True, insert_barriers=True, name='qft')
circuit.measure(q,c)

print(circuit)

correct_bitstring = "11111"
unmitigated = qs_noisy_simulation(circuit, correct_bitstring)
# circuit = qiskit.compiler.transpile(circuit, basis_gates=["u1", "u2", "u3", "cx"])
mitigated = execute_with_zne(circuit, functools.partial(qs_noisy_simulation, correct_bitstring=correct_bitstring))

print(unmitigated)
print(mitigated)

The commented transpile statement (as suggested by @rmlarose in #558) makes it work, but it's not clear to the user that such a step is required for circuits with certain unsupported gates.

I am keeping the discussion here, and not splitting it in multiple issues, because the solution may be general and agnostic of the specific unsupported gate, e.g., @MaldoAlberto's suggestion to support a QASM3-->2 compiler would achieve that 1.

Footnotes

  1. Even though we would rather not go for that solution for the reasons mentioned by Andrea above in the discussion.

@cosenal
Copy link
Contributor

cosenal commented May 7, 2024

@nathanshammah @andreamari Do you think transpiling to a fixed set of basis gates as a fallback each time a gate is not supported could be a viable option? I know that for QEM we generally want to keep the circuit we are manipulating as close as possible to the original input circuit, but perhaps QEM on a transpiled version of the circuit is still better than no QEM whatsoever? Any thoughts?

@nathanshammah
Copy link
Member

For me this could be a viable option, possibly with a warning and with the option to disable this compilation. We'd need to discuss the default.

@cosenal
Copy link
Contributor

cosenal commented May 7, 2024

@nathanshammah OK, great, I will create a separate issue for the transpiling solution than.

@andreamari
Copy link
Member Author

@nathanshammah @andreamari Do you think transpiling to a fixed set of basis gates as a fallback each time a gate is not supported could be a viable option? I know that for QEM we generally want to keep the circuit we are manipulating as close as possible to the original input circuit, but perhaps QEM on a transpiled version of the circuit is still better than no QEM whatsoever? Any thoughts?

Makes sense to me!
Note also that similar transpilations are already implicitly used by Mitiq in other contexts (see e.g. here).

@andreamari
Copy link
Member Author

FYI: I just added the ECRGate to the list of non-supported gates in the description of this issue.
Thanks to @simonecantori for noticing this.

@gluonhiggs
Copy link
Contributor

gluonhiggs commented Nov 25, 2024

@andreamari Is there anything else I can do to fully resolve this issue as you mentioned it in #2579? Apart from addingsx, cu1, PhaseGate, u, and ECR to GATES_TO_DECOMPOSE, are there additional steps or considerations needed to ensure all unsupported gates are properly handled?

@andreamari
Copy link
Member Author

Hi @gluonhiggs, thanks for your interest and help!
I think the change that you mentioned, combined with new unit tests to check that all works as expected, would be a good and useful improvement to the current situation.

The utopic ideal solution would be a faithful conversion of gates, without the need of using fallback decompositions. But we know that this is a non-trivial problem. See e.g. the old comments above in this issue.
A step in this direction is to open issues and/or PRs to Cirq in order to enable the conversion of new gates. Maybe it's just a matter of adding a couple new gates somewhere on this file, but I am not sure.

@gluonhiggs
Copy link
Contributor

gluonhiggs commented Nov 27, 2024

I have added a new PR here. I hope it can fully resolve this without the need to modify Cirq.

Hi @gluonhiggs, thanks for your interest and help! I think the change that you mentioned, combined with new unit tests to check that all works as expected, would be a good and useful improvement to the current situation.

The utopic ideal solution would be a faithful conversion of gates, without the need of using fallback decompositions. But we know that this is a non-trivial problem. See e.g. the old comments above in this issue. A step in this direction is to open issues and/or PRs to Cirq in order to enable the conversion of new gates. Maybe it's just a matter of adding a couple new gates somewhere on this file, but I am not sure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers interface-and-conversions How Mitiq interfaces with quantum software packages. priority/low A non-urgent issue to fix or idea to discuss.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants