# Transpile not working properly for decomposing single qubit gates into Rx Ry and Rz

I used the above code to decompose hadamard gate into RxRyRz but i am getting wrong angles which on multiplying don't match hadamard gate . Interestingly when i am using inbuilt hadamard hate using q.h(0) i am getting correct answer. Please help to find out what is going wrong Code in text form:

    import numpy as np
from qiskit import QuantumCircuit, transpile
from qiskit.quantum_info import Operator
import math

custom_gate_matrix = (1/math.sqrt(2))*np.array([[1, 1],
[1, -1]]
custom_gate = Operator(custom_gate_matrix)
qc = QuantumCircuit(1)
qc.unitary(custom_gate, [0], label='custom_gate')
// qc.h(0)
//print(qc.unitary(custom_gate, [0], label='custom_gate'))
decomposed_circuit = transpile(qc, basis_gates=['rx', 'ry',
'rz'])

rx_angle = 0
ry_angle = 0
rz_angle = 0

for instr, qargs, _ in decomposed_circuit.data:
if instr.name == 'rx':
rx_angle = instr.params[0]
elif instr.name == 'ry':
ry_angle = instr.params[0]
elif instr.name == 'rz':
rz_angle = instr.params[0]

print("Rx angle:", rx_angle)
print("Ry angle:", ry_angle)
print("Rz angle:", rz_angle)


## 1 Answer

This is because a global phase is added during circuit transpilation.

A better option is to use OneQubitEulerDecomposer to get the angles.

from qiskit.synthesis import OneQubitEulerDecomposer

basis = 'ZYZ'
angles = OneQubitEulerDecomposer(basis).angles(custom_gate_matrix)
print(angles)


You will get same values of you do the same with H-gate

from qiskit.circuit.library import HGate

angles = OneQubitEulerDecomposer(basis).angles(HGate().to_matrix())
print(angles)

• Thanks for answering but I want my basis to be RxRyRz which is not supported by OneQubitEulerDecomposer Commented Jun 15 at 13:58