Qiskit / qiskit

Qiskit is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.
https://www.ibm.com/quantum/qiskit
Apache License 2.0
5.11k stars 2.34k forks source link

Qiskit transpiler cannot handle custom multi-qubit gates #5518

Closed nkanazawa1989 closed 2 years ago

nkanazawa1989 commented 3 years ago

What is the expected enhancement?

Because Qiskit is backend agnostic, we should be able to define custom multi qubit gate. What I want to do here is


qc = QuantumCircuit(3)
qc.append(Gate('my_custom_3q_gate', 3, []), [0,1,2])
qc.h(0)
qc.sdg(0)
qc.y(1)
...

qc.add_calibration('my_custom_3q_gate', (0,1,2), my_custom_3q_gate_schedule)

qc = transpile(qc, backend, basis_gates=['u1', 'u2', 'u3', 'cx', 'my_custom_3q_gate'])
sched = schedule(qc, backend)

But current transpiler passes don't allow to have 3q basis gates. I think this limits capability of Qiskit.

https://github.com/Qiskit/qiskit-terra/blob/6646e681501deb8aac55cdc69a8a351ec058e989/qiskit/transpiler/preset_passmanagers/level0.py#L107

aeddins-ibm commented 2 years ago

Just to add motivation to this old issue, I think I'm hitting the same problem when trying to simulate a noisy circuit by adding a multiqubit quantum channel to a circuit. For my use case this is simpler than building the error into a NoiseModel.

I'm using Aer's pauli_error() to define a 3+ qubit error instruction, and including that in a circuit, and later want to transpile the circuit. But AFAICT you can't transpile any circuit to which you've added a 3+ qubit quantum channel, even if that channel is added as a basis gate, which is pretty limiting for noise simulations. This is true even for optimization_level=0. I can limp by without transpiling, but it complicates other parts of the code.

So I think fixing this issue could facilitate a range of noisy simulations.

nkanazawa1989 commented 2 years ago

Thanks for finding this. Indeed current design of pass manager is against the spirit of backend agnostic. Perhaps analyzing basis gates and conditionally inserting the unroll3Q pass is reasonable solution?

mtreinish commented 2 years ago

Yeah unroll3q or more should be basis and target aware. We shouldn't be unrolling gates in the target even if they're > 2q. The caveat here is we'll have to make sure that layout and routing can handle a 3q gate in the circuit. The reason that pass runs first by default is because layout and routing only works with 1q and 2q operations. This connects with https://github.com/Qiskit/qiskit-terra/issues/7812 for making sure the transpiler can work with multiqubit gates