1
$\begingroup$

In qiskit you can get a unitary matrix from a circuit (circuit to unitary matrix example). Is the opposite direction possible? Can you input a unitary matrix and have qiskit come up with a circuit? If it helps you can restrict the matrices to be clifford + multi-qubit controlled pauli strings.

Here's an example that goes from circuit -> unitary and then attempts to get the original circuit back based on the answer below :

import numpy as np
np.set_printoptions(threshold=np.inf)

import qiskit

backend=qiskit.Aer.get_backend('unitary_simulator')

qr=qiskit.QuantumRegister(4,name="qr")

CirA=qiskit.QuantumCircuit(qr);
CirA.cx(3,2)
CirA.h(0)
CirA.cx(0,2)
CirA.h(1)
CirA.cx(1,3)
print(CirA)

job=qiskit.execute(CirA,backend,shots=1)
result=job.result()
MatA=result.get_unitary(CirA,3)

CirB=qiskit.QuantumCircuit(qr);
CirB.unitary(MatA,[ 0, 1, 2, 3 ],label='CirB')
print(CirA)

unroller = qiskit.transpiler.passes.Unroller(basis=['u', 'cx'])

uCirA = qiskit.converters.dag_to_circuit(unroller.run(qiskit.converters.circuit_to_dag(CirA)))
print(uCirA)

uCirB = qiskit.converters.dag_to_circuit(unroller.run(qiskit.converters.circuit_to_dag(CirB)))
print(uCirB)
$\endgroup$

1 Answer 1

2
$\begingroup$

You can easily create a quantum circuit which implements a unitary by appending a UnitaryGate to the circuit, or using QuantumCircuit.unitary() method:

# Get some random unitary:
from qiskit.quantum_info import random_unitary
num_qubits = 4
U = random_unitary(2 ** num_qubits)

# Create the quantum circuit:
qr = QuantumRegister(num_qubits, 'q')
circ = QuantumCircuit(qr)
circ.unitary(U, qr)

For more information about how Qiskit constructs the circuit from the unitary matrix see here.

$\endgroup$
3
  • $\begingroup$ thanks for the answer. I can define a circuit using a unitary as you describe but what I'm really after is the actual synthesis into a gate netlist. In your linked answer you refer to qiskit.quantum_info.synthesis.qsd which is probably what I need to look into. I can't find examples on how to use it. Do you have any? is it restricted to clifford gates? $\endgroup$
    – unknown
    Oct 6, 2022 at 14:38
  • 1
    $\begingroup$ Show the answer here: quantumcomputing.stackexchange.com/a/23547/9474 $\endgroup$ Oct 6, 2022 at 16:14
  • $\begingroup$ I edited the question to experiment with the circuit->unitary->circuit conversions. In principle CirA=CirB, but the unrolled versions (uCirA, uCirB) are different. I would expect them to give the same expansion. Ideally both would have recovered the original circuit which uses only h,cx gates...maybe with a better choice of expansion basis? $\endgroup$
    – unknown
    Oct 6, 2022 at 17:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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