# python code to approximate an arbitrary 2*2 unitary matrix with gates H,T and Z

I'm new to programming, tried different options, compiled programs myself, took them from websites, but they didn't work, for example, the program:

import numpy as np
from qiskit import QuantumCircuit
from qiskit.extensions import HGate, TGate, SGate
from qiskit.quantum_info import Operator
from qiskit.compiler import transpile
from qiskit.transpiler.passes import Unroller
from qiskit.visualization import plot_histogram
from qiskit import Aer

# вентиль, который нужно разложить
matrix = np.array([[1, 1j], [1j, 1]]) / np.sqrt(2)
my_gate = Operator(matrix)

# функция для сравнения двух квантовых операций
def are_operators_close(op1, op2, eps=1e-8):
return np.allclose(op1, op2, rtol=0, atol=eps, equal_nan=False)

# алгоритм Solovay-Kitaev для разложения вентиля в последовательность H, T, S
def sk_decomposition(gate, eps=0.01, depth=3):
# инициализация квантовой схемы
circuit = QuantumCircuit(1)
gates = [HGate(), TGate(), SGate()]

# применение алгоритма Solovay-Kitaev
for d in range(depth):
for gate in gates:
theta = sign * np.arccos(1 - 2 * eps)
circuit.rz(theta, 0)
circuit.append(gate, [0])
circuit.rz(-theta, 0)
eps = eps / 2

# извлечение последовательности вентилей из разложенной матрицы
backend = Aer.get_backend('statevector_simulator')
decomposed_circuit = transpile(circuit, backend=backend, basis_gates=['u', 'h', 'cx', 'id'])
decomposed_result = backend.run(decomposed_circuit).result()
decomposed_state = decomposed_result.get_statevector()
decomposed_matrix = np.outer(decomposed_state, np.conj(decomposed_state))
sequence = ''
for gate in [HGate(), TGate(), SGate()]:
if are_operators_close(decomposed_matrix, gate):
sequence += '0'
else:
for i in range(3):
decomp = np.linalg.matrix_power(gate.to_matrix(), i)
if are_operators_close(decomposed_matrix, decomp):
sequence += str(i)
break

# конвертация последовательности из цифр в последовательность из букв H, T, S
sequence_letters = ''.join(['HTS'[int(x)] for x in sequence])

return sequence_letters

# запуск алгоритма Solovay-Kitaev для вентиля my_gate
sequence = sk_decomposition(my_gate)
print(sequence)


I get an error:

...,line 27, in sk_decomposition
circuit.append(gate(), [0])
TypeError: 'module' object is not callable


how to fix the program or where is there a working code with the Solovey-Kitaev algorithm?

• Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
– Community Bot
May 25, 2023 at 10:14
• This may come in handy: github.com/cryptogoth/skc-python May 25, 2023 at 14:13
• I have a working implementation of the SK algo here: github.com/qcc4cp/qcc/blob/main/src/solovay_kitaev.py May 29, 2023 at 16:29