2
$\begingroup$

I want to use the Qiskit transpile function to decompose an arbitrary unitary matrix/ quantum circuit into a special kind of basis gates. (from qiskit.compiler import transpile)

But for me it seems the only accepted basis gates are 'u1', 'u2', 'u3' and 'cx'. Is there a way to use for example 'rx', 'ry' and 'cx' as basis gates instead of 'u1', 'u2', 'u3' and 'cx'?

The following error

Exception has occurred: QiskitError "Cannot unroll the circuit to the given basis, ['id', 'ry', 'rx', 'cx']. No rule to expand instruction h."

for example is obtained from the code:

from qiskit.quantum_info import Operator
from qiskit.compiler import transpile
import matplotlib.pyplot as plt
from qiskit.compiler.transpile import CouplingMap

coupling_string = [[0, 1], [0,2], [1,2]]

CM = CouplingMap(coupling_string)

qc = QuantumCircuit(3)

qc.h(0)
qc.h(1)
qc.h(2)

result = transpile(qc, coupling_map=CM, basis_gates=['id', 'ry', 'rx', 'cx'], optimization_level=1, seed_transpiler=1) 
result.draw(output='mpl')
plt.show()
$\endgroup$
1
$\begingroup$

Which version of Qiskit are you running? Support for arbitrary basis translation was added very recently in Qiskit 0.20.0/Qiskit-Terra 0.15.0. 1 If you're running an older version of Qiskit then the transpiler will fail like this because the unroller didn't know how to use that basis set. However, when using the latest Qiskit release it will output display a circuit image like:

enter image description here

It's worth noting that the transpilation isn't great because the 1 qubit optimization pass doesn't understand arbitrary basis sets yet and still only works for u1, u2, u3, which is why that output is so large. There is work pending on fixing this https://github.com/Qiskit/qiskit-terra/pull/3658

1 https://qiskit.org/documentation/release_notes.html#qiskit-0-20-0

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thanks for the quick answer. I updated my Qiskit & now I obtain the same result. Would the transpile function work correctly, if a complete Qiskit backend for our machine would be written or is the problem with the 1 qubit optimization pass there also present ? $\endgroup$ – max_keller Oct 8 at 17:08
  • $\begingroup$ If you created a custom backend with the basis gate it would still have the same current limitations. transpile() will just pull the value for basis_gates it passes to the transpiler passes from the backend instead of the kwarg, otherwise it works the same. $\endgroup$ – Matthew Treinish Oct 9 at 11:42
  • $\begingroup$ Ok, I see. Thanks for your help! $\endgroup$ – max_keller Oct 9 at 13:57
  • $\begingroup$ Hi Matthew, I have to ask again some question. Am I right, that the main part of the compilation process (decreasing the number of needed gates/ summing up operations ... mapping them to the available basis gates) is done in the transpile function? If this is true, it seems for me that qiskit can only be effectively used with quantum computers with the basis gate set 'u1', 'u2', 'u3' and 'cx', because otherwise the compilation process would maybe even make it worse. But how can other groups with quantum computers benefit than from qiskit like AQT (Startup around Rainer Blatt)? $\endgroup$ – max_keller Oct 13 at 14:45
  • $\begingroup$ Yeah, the transpile() call is what does hardware embedding and optimization, basically all the circuit transforms. The lack of 1q optimization for other basis sets is definitely a major limitation with qiskit today. .That being said your specific example here is actually a worst case for the current transpiler though, since it's all 1q. If you had a couple of 2q gates in there the transpiler can use unitary decomposition today to optimize things better (it might need the optimization_level=3 kwarg). $\endgroup$ – Matthew Treinish Oct 14 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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