2
$\begingroup$

I have a quantum circuit with a number of gates, and some of them are labeled by some letters without showing the details inside. I have known the quantumcircuit.decompose() would help, but it would decompose all the gates. I am wondering whether we can choose only a subset of gates to decompose. How to write the code? Really appreciate it if some examples can be provided.

$\endgroup$

1 Answer 1

1
$\begingroup$

You can just pass the gate label to decompose method:

op1 = random_unitary(4)
op2 = random_unitary(4)
op3 = random_unitary(4)

circ = QuantumCircuit(2)
circ.unitary(op1, [0, 1], 'label_1')
circ.unitary(op2, [0, 1], 'label_2')
circ.unitary(op3, [0, 1], 'label_3')

circ.decompose(gates_to_decompose = 'label_2').draw('mpl')

enter image description here

gates_to_decompose can be a list such as ['label_1', 'label_2']

$\endgroup$
3
  • $\begingroup$ Thank you very much ! I have tried the following code: qc_Oracle1.decompose(gates_to_decompose = 'abc').draw('mpl'), and got TypeError: decompose() got an unexpected keyword argument 'gates_to_decompose'. Does it mean decompose () does not have the 'gates_to_decompose' argument ? $\endgroup$ Mar 17, 2022 at 13:36
  • $\begingroup$ Check your Qiskit version. gates_to_decompose added in Terra 0.19 (Qiskit 0.33). $\endgroup$ Mar 17, 2022 at 13:49
  • $\begingroup$ It works, thank you! It is indeed the qiskit version that causes the error. $\endgroup$ Mar 17, 2022 at 14:20

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.