Skip to content

Commit

Permalink
chore: implement matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
chensgit169 committed Jan 27, 2024
1 parent 6fc30b5 commit cc0a245
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions quafu/elements/quantum_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


import copy
from abc import ABC, abstractmethod
from abc import ABC
from typing import Callable, Dict, Iterable, List, Optional, Union

import numpy as np
Expand Down Expand Up @@ -156,14 +156,17 @@ def symbol(self, symbol: str):
self._symbol = symbol

@property
@abstractmethod
def matrix(self):
if self._matrix is not None:
def matrix(self) -> ndarray:
"""Return numpy.ndarray matrix representation, real-time updated with parameters, if any."""
if isinstance(self._matrix, ndarray):
return self._matrix
elif isinstance(self._matrix, Callable):
paras = [float(p) for p in self.paras] # TODO: check that if float() is well supported
return self._matrix(paras)
else:
raise NotImplementedError(
"Matrix is not implemented for %s" % self.__class__.__name__
+ ", this should never happen."
+ ". This should never happen, please report it on our github page."
)

def to_qasm(self) -> str:
Expand Down Expand Up @@ -404,11 +407,6 @@ def symbol(self):
def symbol(self, symbol):
self._symbol = symbol

@property
def matrix(self):
# TODO: update matrix when paras of controlled-gate changed
return self._matrix

@property
def ct_nums(self):
targ_num = len(self.targs)
Expand Down

0 comments on commit cc0a245

Please sign in to comment.