Skip to content

Commit

Permalink
adding support for generalr/rx/ry
Browse files Browse the repository at this point in the history
  • Loading branch information
01110011011101010110010001101111 committed Oct 14, 2023
1 parent 8f40ff3 commit a24f0cf
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions torchquantum/layer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@

from .layers import *
from .nlocal import *
from .general import *
88 changes: 88 additions & 0 deletions torchquantum/layer/general.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
"""
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.
"""


import torch
import torchquantum as tq
from torchquantum.layer.layers import (
LayerTemplate0,
Op1QAllLayer,
Op2QAllLayer,
RandomOp1All,
)

__all__ = [
"GlobalR",
"GlobalRX",
"GlobalRY",
"GlobalRZ",
]


class GlobalR(tq.QuantumModule):
"""Layer Template for a Global R General Gate"""

def __init__(
self,
n_wires: int = 0,
theta: float = 0,
phi: float = 0,
):
"""Create the layer"""
super().__init__()
self.ops_all = tq.QuantumModuleList()
self.n_wires = n_wires
self.ops_list = [
{"name": "rot", "params": [phi, theta, 0], "wires": k}
for k in range(self.n_wires)
]

@tq.static_support
def forward(self, q_device):
qmodule = tq.QuantumModule.from_op_history(self.ops_list)
qmodule(q_device)


class GlobalRX(GlobalR):
"""Layer Template for a Global RX General Gate"""

def __init__(
self,
n_wires: int = 0,
theta: float = 0,
):
"""Create the layer"""
super().__init__(n_wires, theta, phi=0)


class GlobalRY(GlobalR):
"""Layer Template for a Global RY General Gate"""

def __init__(
self,
n_wires: int = 0,
theta: float = 0,
):
"""Create the layer"""
super().__init__(n_wires, theta, phi=torch.pi / 2)

0 comments on commit a24f0cf

Please sign in to comment.