Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[unitaryhack] add exponential wrapper class #28

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/orquestra/quantum/circuits/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
ControlledGate,
CustomGateDefinition,
Dagger,
Exponential,
Gate,
GateOperation,
MatrixFactoryGate,
Expand Down
74 changes: 74 additions & 0 deletions src/orquestra/quantum/circuits/_gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ def power(self, exponent: float) -> "Gate":
"""
raise NotImplementedError()

def exp(self) -> "Gate":
"""Gate representing the exponential of the given gate.
"""
raise NotImplementedError()

def bind(self, symbols_map: Dict[sympy.Symbol, Parameter]) -> "Gate":
raise NotImplementedError()

Expand Down Expand Up @@ -214,6 +219,10 @@ def controlled(self, num_controlled_qubits: int) -> Gate:
def dagger(self) -> Union["MatrixFactoryGate", Gate]:
return self if self.is_hermitian else Dagger(self)

@property
def exp(self) -> Gate:
return Exponential(self)

def power(self, exponent: float) -> "Gate":
return Power(self, exponent)

Expand Down Expand Up @@ -298,6 +307,13 @@ def dagger(self) -> "ControlledGate":
num_control_qubits=self.num_control_qubits,
)

@property
def exp(self) -> "ControlledGate":
return ControlledGate(
wrapped_gate=self.wrapped_gate.exp,
num_control_qubits=self.num_control_qubits,
)

def power(self, exponent: float) -> "Gate":
return ControlledGate(
wrapped_gate=self.wrapped_gate.power(exponent),
Expand Down Expand Up @@ -349,6 +365,60 @@ def replace_params(self, new_params: Tuple[Parameter, ...]) -> "Gate":
def dagger(self) -> "Gate":
return self.wrapped_gate

@property
def exp(self) -> "Gate":
return Exponential(self)

def power(self, exponent: float) -> "Gate":
return Power(self, exponent)


EXPONENTIAL_GATE_NAME = "Exponential"


@dataclass(frozen=True)
class Exponential(Gate):
wrapped_gate: Gate

def __post_init__(self):
if len(self.wrapped_gate.free_symbols) > 0:
raise ValueError("On gates with free symbols the exponential cannot be performed")

@property
def matrix(self) -> sympy.Matrix:
return self.wrapped_gate.matrix.exp()

@property
def params(self) -> Tuple[Parameter, ...]:
return self.wrapped_gate.params

@property
def num_qubits(self) -> int:
return self.wrapped_gate.num_qubits

@property
def name(self):
return EXPONENTIAL_GATE_NAME

def controlled(self, num_control_qubits: int) -> Gate:
return self.wrapped_gate.controlled(num_control_qubits).exp

def bind(self, symbols_map) -> "Gate":
raise NotImplementedError(
"Gates exponential do not possess free symbols to bind"
)

def replace_params(self, new_params: Tuple[Parameter, ...]) -> "Gate":
return self.wrapped_gate.replace_params(new_params).exp

@property
def dagger(self) -> "Gate":
return self.wrapped_gate.dagger.exp

@property
def exp(self) -> "Gate":
return Exponential(self)

def power(self, exponent: float) -> "Gate":
return Power(self, exponent)

Expand Down Expand Up @@ -392,6 +462,10 @@ def controlled(self, num_control_qubits: int) -> "Gate":
def dagger(self) -> "Gate":
return self.wrapped_gate.dagger.power(self.exponent)

@property
def exp(self) -> "Gate":
return Exponential(self)

def power(self, exponent: float) -> "Gate":
return Power(self, exponent)

Expand Down
12 changes: 12 additions & 0 deletions src/orquestra/quantum/circuits/_serde.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,14 @@ def _dagger_gate_to_dict(gate: _gates.Dagger):
}


@to_dict.register
def _exponential_gate_to_dict(gate: _gates.Exponential):
return {
"name": gate.name,
"wrapped_gate": to_dict(gate.wrapped_gate),
}


@to_dict.register
def _power_gate_to_dict(gate: _gates.Power):
return {
Expand Down Expand Up @@ -241,6 +249,10 @@ def _special_gate_from_dict(dict_, custom_gate_defs) -> _gates.Gate:
wrapped_gate = _gate_from_dict(dict_["wrapped_gate"], custom_gate_defs)
return _gates.Dagger(wrapped_gate)

elif dict_["name"] == _gates.EXPONENTIAL_GATE_NAME:
wrapped_gate = _gate_from_dict(dict_["wrapped_gate"], custom_gate_defs)
return _gates.Exponential(wrapped_gate)

elif _gates.POWER_GATE_SYMBOL in dict_["name"]:
wrapped_gate = _gate_from_dict(dict_["wrapped_gate"], custom_gate_defs)
return _gates.Power(wrapped_gate, dict_["exponent"])
Expand Down