-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathlightning_qubit.py
178 lines (151 loc) · 5.82 KB
/
lightning_qubit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# Copyright 2021 Xanadu Quantum Technologies Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
r"""
This module contains the :class:`~.LightningQubit` class, a PennyLane simulator device that
interfaces with C++ for fast linear algebra calculations.
"""
from pennylane.devices import DefaultQubit
from .lightning_qubit_ops import apply
import numpy as np
from pennylane import QubitStateVector, BasisState, DeviceError, QubitUnitary
from ._version import __version__
class LightningQubit(DefaultQubit):
"""PennyLane Lightning device.
An extension of PennyLane's built-in ``default.qubit`` device that interfaces with C++ to
perform fast linear algebra calculations.
Use of this device requires pre-built binaries or compilation from source. Check out the
:doc:`/installation` guide for more details.
Args:
wires (int): the number of wires to initialize the device with
shots (int): How many times the circuit should be evaluated (or sampled) to estimate
the expectation values. Defaults to 1000 if not specified.
If ``analytic == True``, then the number of shots is ignored
in the calculation of expectation values and variances, and only controls the number
of samples returned by ``sample``.
analytic (bool): indicates if the device should calculate expectations
and variances analytically
"""
name = "Lightning Qubit PennyLane plugin"
short_name = "lightning.qubit"
pennylane_requires = ">=0.12"
version = __version__
author = "Xanadu Inc."
operations = {
"BasisState",
"QubitStateVector",
"PauliX",
"PauliY",
"PauliZ",
"Hadamard",
"S",
"T",
"CNOT",
"SWAP",
"CSWAP",
"Toffoli",
"CZ",
"PhaseShift",
"RX",
"RY",
"RZ",
"Rot",
"CRX",
"CRY",
"CRZ",
"CRot",
}
kernel_operations = {
"PauliX",
"PauliY",
"PauliZ",
"Hadamard",
"S",
"T",
"RX",
"RY",
"RZ",
"PhaseShift",
"Rot",
"CNOT",
"SWAP",
"CZ",
"CRX",
"CRY",
"CRZ",
"CRot",
"Toffoli",
"CSWAP",
}
observables = {"PauliX", "PauliY", "PauliZ", "Hadamard", "Hermitian", "Identity"}
def __init__(self, wires, *, shots=1000, analytic=True):
super().__init__(wires, shots=shots, analytic=analytic)
@classmethod
def capabilities(cls):
capabilities = super().capabilities().copy()
capabilities.update(
model="qubit",
supports_reversible_diff=False,
supports_inverse_operations=True,
supports_analytic_computation=True,
returns_state=True,
)
capabilities.pop("passthru_devices", None)
return capabilities
def apply(self, operations, rotations=None, **kwargs):
# State preparation is currently done in Python
if operations: # make sure operations[0] exists
if isinstance(operations[0], QubitStateVector):
self._apply_state_vector(operations[0].parameters[0].copy(), operations[0].wires)
del operations[0]
elif isinstance(operations[0], BasisState):
self._apply_basis_state(operations[0].parameters[0], operations[0].wires)
del operations[0]
for operation in operations:
if isinstance(operation, (QubitStateVector, BasisState)):
raise DeviceError(
"Operation {} cannot be used after other Operations have already been "
"applied on a {} device.".format(operation.name, self.short_name)
)
if operations:
self._pre_rotated_state = self.apply_lightning(self._state, operations)
else:
self._pre_rotated_state = self._state
if rotations:
if any(isinstance(r, QubitUnitary) for r in rotations):
super().apply(operations=[], rotations=rotations)
else:
self._state = self.apply_lightning(np.copy(self._pre_rotated_state), rotations)
else:
self._state = self._pre_rotated_state
def apply_lightning(self, state, operations):
"""Apply a list of operations to the state tensor.
Args:
state (array[complex]): the input state tensor
operations (list[~pennylane.operation.Operation]): operations to apply
Returns:
array[complex]: the output state tensor
"""
op_names = [self._remove_inverse_string(o.name) for o in operations]
op_wires = [self.wires.indices(o.wires) for o in operations]
op_param = [o.parameters for o in operations]
op_inverse = [o.inverse for o in operations]
state_vector = np.ravel(state)
apply(state_vector, op_names, op_wires, op_param, op_inverse, self.num_wires)
return np.reshape(state_vector, state.shape)
@staticmethod
def _remove_inverse_string(string):
"""Removes the ``.inv`` appended to the end of inverse gates.
Args:
string (str): name of operation
Returns:
str: name of operation with ``.inv`` removed (if present)
"""
return string.replace(".inv", "")