SimoneGasperini / qiskit-symb

Python package for the symbolic evaluation of parameterized quantum circuits in Qiskit
https://pypi.org/project/qiskit-symb/
Apache License 2.0
26 stars 2 forks source link

Add support for general unparameterized unitary gates #7

Closed awinick closed 8 months ago

awinick commented 8 months ago

Unitary gates help represent arbitrary unparameterized blocks acting on some number of qubits. They are particularly useful during circuit transpilation passes. It is straightforward to support them in qiskit-symb, and I've verified it works as expected. Here is the relevant gate class:

r"""Symbolic unitary gate module"""

from sympy.matrices import Matrix
from ...gate import Gate

class UnitaryGate(Gate):
    r"""Symbolic unitary gate class"""

    def __init__(self, mat, qubits):
        """todo"""
        super().__init__(
            name='unitary',
            num_qubits=len(qubits),
            params=[],
            qubits=qubits
        )
        self.mat = mat

    def __sympy__(self):
        """todo"""
        return Matrix(self.mat)

P.S. Awesome work with everything Simone! Your package has been very helpful and easy to use!

SimoneGasperini commented 8 months ago

Hi Adam and thanks for posting this! I just added a basic implementation of the UnitaryGate class as you suggested. Let me know if it makes sense to you. Also, could you please provide an example about a specific use-case of this feature? It could be really helpful for me to better understand your needs and possibly further improve the current implementation ;)

EDIT: fixed in faf4edb

awinick commented 8 months ago

Thanks for adding this so quickly - exactly what I had in mind!

I've been working on modifying ConsolidateBlocks so it can be applied directly to parametric circuits. During circuit transpilation, a situation can arise where I need to consolidate a unitary and some parametric gates.

I have a class that uses qiskit-sim under the hood to store and operate on symbolic consolidated blocks and assign parameters when desired. Your package greatly simplified the necessary work, but without support for unitary gates, it failed for some circuits.

I can't be too specific, but I am happy to try and answer any more questions you might have. I'll also let you know if I find any other features that might be useful.

SimoneGasperini commented 8 months ago

Oh ok cool! Thank you very much for the feedback, I'm glad to hear that you found the package useful. And yes, don't hesitate to let me know if you have in mind any other feature that qiskit-symb should offer.

I close this issue as completed now but feel free to reopen it if you need something more related to the UnitaryGate support.