Skip to content

gates

Gates.

Gate

Source code in jaxquantum/circuits/gates.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
@struct.dataclass
class Gate:
    dims: List[int] = struct.field(pytree_node=False)
    _U: Optional[Array]  # Unitary
    _H: Optional[Array]  # Hamiltonian
    _KM: Optional[Qarray]  # Kraus map
    _params: Dict[str, Any]
    _ts: Array
    _name: str = struct.field(pytree_node=False)
    num_modes: int = struct.field(pytree_node=False)

    @classmethod
    def create(
        cls,
        dims: Union[int, List[int]],
        name: str = "Gate",
        params: Optional[Dict[str, Any]] = None,
        ts: Optional[Array] = None,
        gen_U: Optional[Callable[[Dict[str, Any]], Qarray]] = None,
        gen_H: Optional[Callable[[Dict[str, Any]], Qarray]] = None,
        gen_KM: Optional[Callable[[Dict[str, Any]], List[Qarray]]] = None,
        num_modes: int = 1,
    ):
        """Create a gate.

        Args:
            dims: Dimensions of the gate.
            name: Name of the gate.
            params: Parameters of the gate.
            ts: Times of the gate.
            gen_U: Function to generate the unitary of the gate.
            gen_H: Function to generate the Hamiltonian of the gate.
            gen_KM: Function to generate the Kraus map of the gate.
            num_modes: Number of modes of the gate.
        """

        # TODO: add params to device?

        if isinstance(dims, int):
            dims = [dims]

        assert len(dims) == num_modes, (
            "Number of dimensions must match number of modes."
        )

        # Unitary
        _U = gen_U(params) if gen_U is not None else None
        _H = gen_H(params) if gen_H is not None else None

        if gen_KM is not None:
            _KM = gen_KM(params)
        elif _U is not None:
            _KM = Qarray.from_list([_U])

        return Gate(
            dims=dims,
            _U=_U,
            _H=_H,
            _KM=_KM,
            _params=params if params is not None else {},
            _ts=ts if ts is not None else jnp.array([]),
            _name=name,
            num_modes=num_modes,
        )

    def __str__(self):
        return self._name

    def __repr__(self):
        return self._name

    @property
    def U(self):
        return self._U

    @property
    def H(self):
        return self._H

    @property
    def KM(self):
        return self._KM

create(dims, name='Gate', params=None, ts=None, gen_U=None, gen_H=None, gen_KM=None, num_modes=1) classmethod

Create a gate.

Parameters:

Name Type Description Default
dims Union[int, List[int]]

Dimensions of the gate.

required
name str

Name of the gate.

'Gate'
params Optional[Dict[str, Any]]

Parameters of the gate.

None
ts Optional[Array]

Times of the gate.

None
gen_U Optional[Callable[[Dict[str, Any]], Qarray]]

Function to generate the unitary of the gate.

None
gen_H Optional[Callable[[Dict[str, Any]], Qarray]]

Function to generate the Hamiltonian of the gate.

None
gen_KM Optional[Callable[[Dict[str, Any]], List[Qarray]]]

Function to generate the Kraus map of the gate.

None
num_modes int

Number of modes of the gate.

1
Source code in jaxquantum/circuits/gates.py
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
@classmethod
def create(
    cls,
    dims: Union[int, List[int]],
    name: str = "Gate",
    params: Optional[Dict[str, Any]] = None,
    ts: Optional[Array] = None,
    gen_U: Optional[Callable[[Dict[str, Any]], Qarray]] = None,
    gen_H: Optional[Callable[[Dict[str, Any]], Qarray]] = None,
    gen_KM: Optional[Callable[[Dict[str, Any]], List[Qarray]]] = None,
    num_modes: int = 1,
):
    """Create a gate.

    Args:
        dims: Dimensions of the gate.
        name: Name of the gate.
        params: Parameters of the gate.
        ts: Times of the gate.
        gen_U: Function to generate the unitary of the gate.
        gen_H: Function to generate the Hamiltonian of the gate.
        gen_KM: Function to generate the Kraus map of the gate.
        num_modes: Number of modes of the gate.
    """

    # TODO: add params to device?

    if isinstance(dims, int):
        dims = [dims]

    assert len(dims) == num_modes, (
        "Number of dimensions must match number of modes."
    )

    # Unitary
    _U = gen_U(params) if gen_U is not None else None
    _H = gen_H(params) if gen_H is not None else None

    if gen_KM is not None:
        _KM = gen_KM(params)
    elif _U is not None:
        _KM = Qarray.from_list([_U])

    return Gate(
        dims=dims,
        _U=_U,
        _H=_H,
        _KM=_KM,
        _params=params if params is not None else {},
        _ts=ts if ts is not None else jnp.array([]),
        _name=name,
        num_modes=num_modes,
    )