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 all 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
76 changes: 76 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()

@property
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) -> "Gate":
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,62 @@ 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 +464,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
75 changes: 74 additions & 1 deletion tests/orquestra/quantum/circuits/_gates_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
import sympy

from orquestra.quantum.circuits import _builtin_gates, _gates
from orquestra.quantum.circuits._gates import GateOperation, MatrixFactoryGate, Power
from orquestra.quantum.circuits._gates import (
Exponential,
GateOperation,
MatrixFactoryGate,
Power,
)

GATES_REPRESENTATIVES = [
_builtin_gates.X,
Expand Down Expand Up @@ -111,6 +116,10 @@ def test_daggers_matrix_is_adjoint_of_original_gates_matrix(self):
gate = MatrixFactoryGate("V", example_one_qubit_matrix_factory, (1, 2), 1)
assert gate.dagger.matrix == gate.matrix.adjoint()

def test_matrix_exponential_is_exponential_of_original_gates_matrix(self):
gate = MatrixFactoryGate("V", example_one_qubit_matrix_factory, (1, 2), 1)
assert gate.exp.matrix == gate.matrix.exp()

def test_dagger_has_the_same_params_and_num_qubits_as_wrapped_gate(self):
gate = MatrixFactoryGate(
"U", example_two_qubit_matrix_factory, (0.5, 0.1, sympy.Symbol("a")), 2
Expand All @@ -128,6 +137,10 @@ def test_power_of_dagger_is_dagger_wrapped_by_power(self):
gate = MatrixFactoryGate("V", example_one_qubit_matrix_factory, (1, 0), 1)
assert gate.dagger.power(0.5) == Power(gate.dagger, 0.5)

def test_exponential_of_dagger_is_dagger_wrapped_by_exponential(self):
gate = MatrixFactoryGate("V", example_one_qubit_matrix_factory, (1, 0), 1)
assert gate.dagger.exp == Exponential(gate.dagger)

def test_binding_gates_in_dagger_is_propagated_to_wrapped_gate(self):
theta = sympy.Symbol("theta")
gate = MatrixFactoryGate("V", example_one_qubit_matrix_factory, (theta, 0), 1)
Expand Down Expand Up @@ -199,6 +212,11 @@ def test_dagger_of_controlled_gate_is_controlled_gate_wrapping_dagger(self, gate

assert controlled_gate.dagger == gate.dagger.controlled(4)

def test_exp_of_controlled_gate_is_controlled_gate_wrapping_exp(self, gate):
if len(gate.free_symbols) == 0:
controlled_gate = gate.controlled(2)
assert controlled_gate.exp == gate.exp.controlled(2)

def test_power_of_controlled_gate_is_controlled_gate_wrapping_power(self, gate):
if len(gate.free_symbols) == 0:
controlled_gate = gate.controlled(2)
Expand Down Expand Up @@ -288,6 +306,61 @@ def test_constructing_power_gate_and_replacing_parameters_commute(
).power(exponent)


@pytest.mark.parametrize("gate", GATES_REPRESENTATIVES[:10])
class TestGateExponential:
def test_constructing_a_gate_exponential_with_free_symbols_raises_error(self, gate):
if len(gate.free_symbols) > 0:
with pytest.raises(ValueError):
gate.exp

def test_gate_exponential_naming_scheme(self, gate):
if len(gate.free_symbols) == 0:
gate_exponential = gate.exp
assert gate_exponential.name == _gates.EXPONENTIAL_GATE_NAME

def test_has_same_parameters_as_wrapped_gate(self, gate):
if len(gate.free_symbols) == 0:
assert gate.exp.params == gate.params

def test_has_same_free_symbols_as_wrapped_gate(self, gate):
if len(gate.free_symbols) == 0:
assert gate.exp.free_symbols == gate.free_symbols

def test_has_same_number_of_qubits_as_wrapped_gate(self, gate):
if len(gate.free_symbols) == 0:
assert gate.exp.num_qubits == gate.num_qubits

def test_matrix_exponential_equal_to_wrapped_gate_matrix_exponential(self, gate):
if len(gate.free_symbols) == 0 and gate.name != "T":
wrapped_gate_matrix_exponential = gate.matrix.exp()
assert gate.exp.matrix == wrapped_gate_matrix_exponential

def test_dagger_of_gate_exponential_gate_exponential_of_dagger(self, gate):
if len(gate.free_symbols) == 0 and gate.name != "T":
assert gate.exp.dagger == gate.dagger.exp

def test_power_of_gate_exponential_gate_exponential_of_power(self, gate):
if len(gate.free_symbols) == 0 and gate.name != "T":
assert gate.exp.power(2.0) != gate.power(2.0).exp

def test_parameter_binding_not_implemented_for_gate_exponential(self, gate):
if len(gate.free_symbols) == 0:
gate_exponential = gate.exp
symbols_map = {sympy.Symbol("theta"): 0.5, sympy.Symbol("x"): 3}
with pytest.raises(NotImplementedError):
gate_exponential.bind(symbols_map)

def test_constructing_gate_exponential_and_replacing_parameters_commute(self, gate):
if len(gate.free_symbols) == 0:
gate_exponential = gate.exp
new_params = tuple(3 * param for param in gate_exponential.params)

assert (
gate_exponential.replace_params(new_params)
== gate.replace_params(new_params).exp
)


@pytest.mark.parametrize("gate", GATES_REPRESENTATIVES)
class TestGateOperation:
def test_bound_symbols_are_not_present_in_gate_parameters(self, gate):
Expand Down
11 changes: 11 additions & 0 deletions tests/orquestra/quantum/circuits/_serde_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@
_builtin_gates.T.dagger(7),
]
),
_circuit.Circuit(
[
_builtin_gates.Y.exp(1),
]
),
_circuit.Circuit(
[
_builtin_gates.RX(-np.pi).dagger(2),
Expand All @@ -118,6 +123,12 @@
_builtin_gates.PHASE(np.pi / 5).dagger(2),
]
),
_circuit.Circuit(
[
_builtin_gates.RX(-np.pi).exp(2),
_builtin_gates.RY(-np.pi / 2).exp(1),
]
),
_circuit.Circuit(
[
_builtin_gates.RX(GAMMA * ALPHA).dagger(1),
Expand Down