Unitary matrix error

I use pennylane and qiskit to build a same circuit. But I get different unitary matrix. Can you help me with this. By the way, I use bqskit to get the qiskit circuit unitary matrix. The code is as follows:

def circuit_pennylane(w):
    
    qml.U3(*w[0: 3], wires=0)
    qml.U3(*w[3: 6], wires=2)
    qml.U3(*w[6: 9], wires=3)

    qml.CNOT(wires=[4, 5])
    qml.CNOT(wires=[6, 7])
    qml.CNOT(wires=[0, 1])
    qml.CNOT(wires=[2, 3])

    qml.U3(*w[9: 12], wires=4)
    qml.U3(*w[12: 15], wires=7)
    qml.U3(*w[15: 18], wires=1)
    qml.U3(*w[18: 21], wires=3)

    qml.CNOT(wires=[4, 5])
    qml.CNOT(wires=[6, 7])
    qml.CNOT(wires=[0, 1])
    qml.CNOT(wires=[2, 3])

    qml.U3(*w[21: 24], wires=4)
    qml.U3(*w[24: 27], wires=5)
    qml.U3(*w[27: 30], wires=7)
    qml.U3(*w[30: 33], wires=1)
    qml.U3(*w[33: 36], wires=3)

def circuit_qiskit(w):

    qr = qiskit.circuit.QuantumRegister(8)
    circuit_qiskit = qiskit.circuit.QuantumCircuit(qr)
    circuit_qiskit.u3(*w[0: 3], qr[0])
    circuit_qiskit.u3(*w[3: 6], qr[2])
    circuit_qiskit.u3(*w[6: 9], qr[3])

    circuit_qiskit.cx(qr[4], qr[5])
    circuit_qiskit.cx(qr[6], qr[7])
    circuit_qiskit.cx(qr[0], qr[1])
    circuit_qiskit.cx(qr[2], qr[3])

    circuit_qiskit.u3(*w[9: 12], qr[4])
    circuit_qiskit.u3(*w[12: 15], qr[7])
    circuit_qiskit.u3(*w[15: 18], qr[1])
    circuit_qiskit.u3(*w[18: 21], qr[3])

    circuit_qiskit.cx(qr[4], qr[5])
    circuit_qiskit.cx(qr[6], qr[7])
    circuit_qiskit.cx(qr[0], qr[1])
    circuit_qiskit.cx(qr[2], qr[3])

    circuit_qiskit.u3(*w[21: 24], qr[4])
    circuit_qiskit.u3(*w[24: 27], qr[5])
    circuit_qiskit.u3(*w[27: 30], qr[7])
    circuit_qiskit.u3(*w[30: 33], qr[1])
    circuit_qiskit.u3(*w[33: 36], qr[3])
    return circuit_qiskit

com_params = np.array([0.5017826429101869, 3.6367716666551555, 2.7505603097266587,
5.2689878096886, 1.9469336295216628, -1.3368441720095896,
0.513074271033224, 4.199584296075159, 6.306066549892076,
2.7683481555222893, 0.7127131166893816, -0.06444712800830774,
-28.083197394636084, 4.7158324361873225, 1.5702730577895188,
3.5346400976751595, 3.2665036868214488, 2.128460281389509,
-3.9528591944637474, 5.333712463704893, 2.60882112672375,
4.391537489903239, 5.632487318517943, 3.779765489939559,
3.91163645661754, 1.2912910442439236, 7.512955104697662,
30.343523516097846, 6.172208326683445, 0.1080466303692923,
1.3628332318551757, 6.399627690003927, 0.667632291595952,
2.6854448789723238, 2.2782012039524724, 6.937858784422129])


get_matrix = qml.matrix(circuit_pennylane)
pennylane_matrix = get_matrix(com_params)


circuit = circuit_qiskit(com_params)
qiskit_matrix = qiskit_to_bqskit(circuit).get_unitary().numpy

A = qiskit_matrix
B = pennylane_matrix
epsilon = np.abs(1 - np.abs(np.sum(np.multiply(A,np.conj(B)))) / A.shape[0])
print(epsilon)
1 Like

Hey @cheng! Welcome back :slight_smile:

I can’t replicate the behaviour you’re seeing because I’m missing qiskit_to_bqskit. If you could attach some code with how it’s defined, that would be awesome!

Hi,

Thanks for your reply. I should apologize for the missing code. You should use the follow code to import qiskit_to_bqskit.

from bqskit.ext import qiskit_to_bqskit

Before this, maybe you should install the bqskit. The link is for the installation.
Getting Started — BQSKit documentation.

And I have test several gates. In my opinion, this is only happend when I use qml.U3 gate. I have check the single U3 gate unitary matrix in qiskit, pennylane and bqskit. I think they are the same. But when you combine U3 gate with other gates, such as CNOT. The unitary matrix of whole circuit goes wrong. This is what I do, and what I know. Please check it.

Thanks! I honestly can’t say what’s wrong or right with the package you’re using (bqskit). It could be that it’s tacking on a global phase under the hood or something completely different. All I can say is that I trust the qml.matrix transformation :sweat_smile:.

Is there a reason you need bqskit to obtain the matrix of a qiskit circuit? Seems like it should be doable with native qiskit functions: Checking the unitary matrix of the quantum circuit on Qiskit | by Yuichiro Minato | Blueqat (blueqat Inc. / former MDR Inc.) | Medium

Let me know if this helps!

Hi,

Thanks for your reply and patience. I use bqskit to synthesize the circuit. Maybe I made some mistakes. I will check my code again. Thanks again.

Interesting! Please get back to me if you’re still not able to figure out what’s going on :slight_smile: