I am a beginner when it comes to quantum computing so forgive me if this is a dumb question. Does anyone know how to create a gate from any matrix on OpenQasm2? Specifically, can anyone provide any tips on how to convert a matrix to Qubit rotations using the U gate in OpenQasm2? Also, could you form an addition gate using a similarly designed matrix in this answer


1 Answer 1


Here's one example of a way to do it programmatically:

from cirq import Circuit, LineQubit, ops

from qbraid.interface import random_unitary_matrix
from qbraid.transpiler.cirq_qasm import to_qasm

n_qubits = 1
matrix = random_unitary_matrix(dim=2**n_qubits)

qubits = [LineQubit(i) for i in range(n_qubits)]
circuit = Circuit(ops.MatrixGate(matrix)(*qubits))

qasm_str = to_qasm(circuit)

      ┌                           ┐
0: ───│-0.255-0.574j  0.266-0.731j│───
      │-0.644-0.436j  0.131+0.615j│
      └                           ┘

// Generated from qBraid v0.4.0

include "qelib1.inc";

// Qubits: [q(0)]
qreg q[1];

u3(pi*1.4327602146,pi*0.822476743,pi*0.2440365932) q[0];

Can then track the qbraid.transpiler.cirq_qasm.to_qasm source code back through cirq.QasmOutput and break down the procedure step-by-step.


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.