3
$\begingroup$

It's well known that the single-qubit rotation gate $U(\theta, \phi, \lambda)$ and the controlled-not gate $CX_{i,j}$ form one of the universal set of gates for quantum computation. So here's my question: is the Qiskit transpile function able to compile any given quantum circuit by using u and cx gates only?

In particular, I would assume that the following code will always work with no errors:

from qiskit.circuit.random import random_circuit
from qiskit.compiler import transpile

qc = random_circuit(num_qubits=5, depth=10)
tqc = transpile(qc, basis_gates=['u', 'cx'])

Unfortunately, it would seem not to be so and I was wondering why. I also noticed that, if I add the identity gate id in the list of basis_gates, the transpilation looks to always run successfully and I'm not sure about the reason for this.

The error produced by qiskit is:

QiskitError: "Cannot unroll the circuit to the given basis, ['u', 'cx'].
Instruction id not found in equivalence library and no rule found to expand."
$\endgroup$
2
  • 1
    $\begingroup$ Do you get an error ? If so it would be helpful to see the traceback. Gates 'u' and 'cx' worked for me when I was transpiling. $\endgroup$
    – MonteNero
    Jan 26 at 15:23
  • 1
    $\begingroup$ Sometimes it works, others it raises an error. For instance, try random_circuit(num_qubits=5, depth=10, seed=1): you should be able to reproduce the issue (I'm using Qiskit Terra version 0.22.3). $\endgroup$ Jan 26 at 15:45

1 Answer 1

1
$\begingroup$

The basis gates u, cx are universal. This error is an artifact of the Qiskit transpiler. Change your transpiler call line to this:

tqc = transpile(qc, basis_gates=['u', 'cx', 'id'])

And the error should be fixed.

The reason this error occurs is because the identity gate (called id) can be used as a sort of timing enforcement gate for when the circuit is executing/or compiling. But, of course it is not actually needed in order to completely specify any intended quantum circuit. I assume in future versions of Qiskit this may be changed somehow so that users can be more selective for when they want to strictly include id gates or not.

By the way, this may actually be a bug in some cases (you should check this for your test cases), for example if id is not ever used by the transpiler for an example random circuit. In this case you should report this as a bug in the Qiskit Github.

$\endgroup$
5
  • $\begingroup$ I didn't quite understand your explanation about the id gate and why it should be in the basis set. But to me, it looks like a bug. +1 anyways. $\endgroup$
    – MonteNero
    Jan 26 at 23:48
  • 1
    $\begingroup$ I'm also trying to understand why this isn't a flat-out bug. If the transpiler needs "id" for internal purposes, then it can add them and then delete them. What am I missing? $\endgroup$ Jan 27 at 0:43
  • $\begingroup$ Thank you Elijah. I knew about the id gate as a "delay cycle" (see also qiskit-terra issue#4671) but I'm still not sure that's the only explanation for this problem. Indeed, if it was, I would expect the same error raised for any quantum circuit; instead, if you try for example random_circuit(num_qubits=5, depth=10, seed=2) the transpiler does the job without complaining. $\endgroup$ Jan 27 at 10:40
  • 1
    $\begingroup$ This is because random_circuit(num_qubits=5, depth=10, seed=1) returns a circuit contains I-gate, while random_circuit(num_qubits=5, depth=10, seed=2) don't. You can easily check this by drawing them: random_circuit(num_qubits=5, depth=10, seed=1).draw('mpl'). $\endgroup$ Jan 27 at 11:58
  • $\begingroup$ Ah ok as easy as that. So, maybe, simply adding the equivalence $I = U(0, 0, 0)$ to the Qiskit equivalence_library should solve the problem. $\endgroup$ Jan 27 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.