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

Allow fixed parameters in GeneralEncoder #252

Merged
merged 3 commits into from
Nov 18, 2024
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
81 changes: 81 additions & 0 deletions test/encoding/test_encodings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
"""
MIT License

Copyright (c) 2020-present TorchQuantum Authors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""

# test the controlled unitary function


import torchquantum as tq
import torch
from test.utils import check_all_close


def test_GeneralEncoder():

parameterised_funclist = [
{"input_idx": [0], "func": "crx", "wires": [1, 0]},
{"input_idx": [1, 2, 3], "func": "u3", "wires": [1]},
{"input_idx": [4], "func": "ry", "wires": [0]},
{"input_idx": [5], "func": "ry", "wires": [1]},
]

semiparam_funclist = [
{"params": [0.2], "func": "crx", "wires": [1, 0]},
{"params": [0.3, 0.4, 0.5], "func": "u3", "wires": [1]},
{"input_idx": [0], "func": "ry", "wires": [0]},
{"input_idx": [1], "func": "ry", "wires": [1]},
]

expected_states = torch.complex(
torch.Tensor(
[[0.8423, 0.4474, 0.2605, 0.1384], [0.7649, 0.5103, 0.3234, 0.2157]]
),
torch.Tensor(
[[-0.0191, 0.0522, -0.0059, 0.0162], [-0.0233, 0.0483, -0.0099, 0.0204]]
),
)

parameterised_enc = tq.GeneralEncoder(parameterised_funclist)
semiparam_enc = tq.GeneralEncoder(semiparam_funclist)

param_vec = torch.Tensor(
[[0.2, 0.3, 0.4, 0.5, 0.6, 0.7], [0.2, 0.3, 0.4, 0.5, 0.8, 0.9]]
)
semiparam_vec = torch.Tensor([[0.6, 0.7], [0.8, 0.9]])

qd = tq.QuantumDevice(n_wires=2)

qd.reset_states(bsz=2)
parameterised_enc(qd, param_vec)
state1 = qd.get_states_1d()

qd.reset_states(bsz=2)
semiparam_enc(qd, semiparam_vec)
state2 = qd.get_states_1d()

check_all_close(state1, state2)
check_all_close(state1, expected_states)


if __name__ == "__main__":
test_GeneralEncoder()
18 changes: 17 additions & 1 deletion torchquantum/encoding/encodings.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,18 @@ class GeneralEncoder(Encoder, metaclass=ABCMeta):
{'input_idx': [12, 13, 14], 'func': 'u3', 'wires': [3]},
{'input_idx': [15], 'func': 'u1', 'wires': [3]},
]

Example 3:
[
{'params': [0.25], 'func': 'rx', 'wires': [0]},
{'params': [0.25], 'func': 'rx', 'wires': [1]},
{'params': [0.25], 'func': 'rx', 'wires': [2]},
{'params': [0.25], 'func': 'rx', 'wires': [3]},
{'input_idx': [0], 'func': 'ry', 'wires': [0]},
{'input_idx': [1], 'func': 'ry', 'wires': [1]},
{'input_idx': [2], 'func': 'ry', 'wires': [2]},
{'input_idx': [3], 'func': 'ry', 'wires': [3]}
]
"""

def __init__(self, func_list):
Expand All @@ -90,7 +102,11 @@ def __init__(self, func_list):
def forward(self, qdev: tq.QuantumDevice, x):
for info in self.func_list:
if tq.op_name_dict[info["func"]].num_params > 0:
params = x[:, info["input_idx"]]
# If params are provided in encoder, use those,
# else use params from x
params = (torch.Tensor(info["params"]).repeat(x.shape[0], 1)
if info.get("params")
else x[:, info["input_idx"]])
else:
params = None
func_name_dict[info["func"]](
Expand Down
Loading