1

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)

unknown
  • 2,052
  • 1
  • 7
  • 16

1 Answers1

2

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.

Egretta.Thula
  • 9,972
  • 1
  • 11
  • 30
  • 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? – unknown Oct 06 '22 at 14:38
  • 1
    Show the answer here: https://quantumcomputing.stackexchange.com/a/23547/9474 – Egretta.Thula Oct 06 '22 at 16:14
  • 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? – unknown Oct 06 '22 at 17:56