Qiskit / qiskit

Qiskit is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.
https://www.ibm.com/quantum/qiskit
Apache License 2.0
4.91k stars 2.31k forks source link

Unitary Matrix Gates #10764

Closed Oussema-t closed 10 months ago

Oussema-t commented 11 months ago

Environment

What is happening?

I try to add those gates of unitary matrix in new circuit, but still have error.

How can we reproduce the issue?

I can share my code:

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from numpy import pi
import numpy as np
from qiskit import Aer
from qiskit import QuantumCircuit, execute, Aer, visualization
from qiskit import QuantumCircuit, transpile, assemble, Aer, execute
from qiskit.visualization import plot_histogram

from qiskit import QuantumCircuit
# Create your quantum circuit
qreg_q = QuantumRegister(2, 'q')
creg_c0 = ClassicalRegister(2, 'c0')
circuit = QuantumCircuit(qreg_q,creg_c0)
#qc.h(0)
#qc.cx(0, 1)
circuit.h(qreg_q[0])
circuit.cx(qreg_q[0], qreg_q[1])
#qc.x(2)
# Add more gates to define your circuit
# Simulate the circuit and get the unitary matrix
simulator = Aer.get_backend('unitary_simulator')
result = execute(circuit, simulator).result()
unitary_matrix = result.get_unitary(circuit)
print(unitary_matrix)
from qiskit.extensions import UnitaryGate
from qiskit.quantum_info.synthesis import two_qubit_cnot_decompose

# Decompose the unitary matrix into gates
gate_decomposition = two_qubit_cnot_decompose(unitary_matrix)
gate_decomposition.draw()
# Create a QuantumCircuit from the gate decomposition
gate_counts = gate_decomposition.count_ops()
print(gate_counts)
decomposed_circuit = QuantumCircuit(2)
for gate in gate_decomposition:
    decomposed_circuit.append(gate,[0,1])

What should happen?

The function (append) should add all gates from the unitary matrix to the necircuit

Any suggestions?

The function doesn't work, we should modify the parameters or something like that !

jakelishman commented 11 months ago

This is because you're passing an incorrect object to QuantumCircuit.append. The append method takes an Instruction instance followed by a list of qubit specifiers, followed by a list of clbit specifiers, but you're instead passing a complete CircuitInstruction context object from a different circuit in the first argument.

From what you've written, the circuit gate_decomposition already appears to be the circuit you are trying to construct. Alternatively, you can merge one circuit into another while specifying the new qubits to act on using QuantumCircuit.compose.

Oussema-t commented 10 months ago

Thank you so much for your response, that was amazing from your part. I decompose my circuit using "gate_decomposition = two_qubit_cnot_decompose(unitary_matrix) " and run the second circuit, but still have different outputs from the second and the first one. I use the same simulator, but the state vectors for the first and the second are not the same !

jakelishman commented 10 months ago

I'm not sure what you mean by the statevectors not being equal; up to numerical tolerance, Aer produces the same unitary for both your circuit and your gate_decomposition as I see it.

Oussema-t commented 10 months ago

image image

Oussema-t commented 10 months ago

The first one is the main circuit, and the second is the decomposed circuit !

Oussema-t commented 10 months ago

When the two circuits are equal, they will provide the same state vectors?

Oussema-t commented 10 months ago

image image image The unitary Matrixes are not equal !

jakelishman commented 10 months ago

You can't expect bit-for-bit equality when the floating-point operations being performed are different, even if in "perfect" real-number mathematics they would produce the exact same result. In this case, both the decomposition itself and then the subsequent matrix multiplications to produce the unitary matrix have small error terms in them, and that's what you're seeing.

If you look at the elementwise magnitude of the difference, you can see that it's all on the order of 1e-15, which is about what should be expected.

Oussema-t commented 10 months ago

Thank you man, I didn't see the e-17!

Oussema-t commented 10 months ago

Other question please ? Can we apply the decomposer for more than 2 qubits ?

jakelishman commented 10 months ago

Synthesis is a non-trivial problem in general; if you want general unitary synthesis for any number of qubits, you're most likely better off using a full compilation (transpile) defining the target basis set, etc.

I'm going to close this issue now, because the problem is solved. If you have usage questions, I'd encourage you to take them to the Qiskit slack.