Skip to content

Commit

Permalink
Add UnitaryGate class as suggested by @awinick in issue #7
Browse files Browse the repository at this point in the history
  • Loading branch information
SimoneGasperini committed Jan 2, 2024
1 parent 11d9327 commit 5573b3e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/qiskit_symb/circuit/library/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
TGate, TdgGate, CTGate, CTdgGate,
SwapGate, CSwapGate,
iSwapGate, CiSwapGate,
ECRGate, CECRGate
ECRGate, CECRGate,
UnitaryGate
)

from .parametric_gates import (
Expand Down Expand Up @@ -76,5 +77,6 @@
'iswap': iSwapGate,
'ciswap': CiSwapGate,
'ecr': ECRGate,
'cecr': CECRGate
'cecr': CECRGate,
'unitary': UnitaryGate
}
1 change: 1 addition & 0 deletions src/qiskit_symb/circuit/library/standard_gates/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
from .swap import SwapGate, CSwapGate
from .iswap import iSwapGate, CiSwapGate
from .ecr import ECRGate, CECRGate
from .unitary import UnitaryGate
20 changes: 20 additions & 0 deletions src/qiskit_symb/circuit/library/standard_gates/unitary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""Symbolic generic unitary gate module"""

import numpy as np
from sympy.matrices import Matrix
from ...gate import Gate


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

def __init__(self, matrix):
"""todo"""
num_qubits = int(np.log2(len(matrix)))
qubits = list(range(num_qubits))
super().__init__(name='unitary', num_qubits=num_qubits, params=[], qubits=qubits)
self.matrix = matrix

def __sympy__(self):
"""todo"""
return Matrix(self.matrix)
18 changes: 16 additions & 2 deletions tests/test_standard_gates.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
"""Test standard gates module"""

import numpy
from hypothesis import given, strategies
from qiskit.quantum_info import random_unitary
from qiskit.circuit.library import (
IGate, XGate, YGate, ZGate,
HGate, SXGate, SXdgGate,
SGate, SdgGate, TGate, TdgGate,
SwapGate, iSwapGate, ECRGate
SwapGate, iSwapGate, ECRGate,
UnitaryGate
)
from qiskit_symb.circuit.library import (
IGate as symb_IGate,
Expand All @@ -21,7 +24,8 @@
TdgGate as symb_TdgGate,
SwapGate as symb_SwapGate,
iSwapGate as symb_iSwapGate,
ECRGate as symb_ECRGate
ECRGate as symb_ECRGate,
UnitaryGate as symb_UnitaryGate
)


Expand Down Expand Up @@ -121,3 +125,13 @@ def test_ecr():
arr1 = ECRGate().to_matrix()
arr2 = symb_ECRGate().to_numpy()
assert numpy.allclose(arr1, arr2)


@given(num_qubits=strategies.integers(min_value=1, max_value=3),
seed=strategies.integers(min_value=0))
def test_unitary(num_qubits, seed):
"""todo"""
random_unitary_matrix = random_unitary(dims=2**num_qubits, seed=seed).data
arr1 = UnitaryGate(data=random_unitary_matrix).to_matrix()
arr2 = symb_UnitaryGate(matrix=random_unitary_matrix).to_numpy()
assert numpy.allclose(arr1, arr2)

0 comments on commit 5573b3e

Please sign in to comment.