pyqtorch
exposes three API endpoints called run
, sample
and expectation
.
Please note that all endpoints expect a QuantumCircuit
object.
run
Sequentially apply each operation in circuit
to an input state state
given parameter values values
, perform an optional embedding
on values
and return an output state.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
circuit
|
QuantumCircuit
|
A pyqtorch.QuantumCircuit instance. |
required |
state
|
Tensor
|
A torch.Tensor of shape [2, 2, ..., batch_size]. |
None
|
values
|
dict[str, Tensor] | None
|
A dictionary containing <'parameter_name': torch.Tensor> pairs denoting
the current parameter values for each parameter in |
None
|
embedding
|
Embedding | None
|
An optional instance of |
None
|
Returns:
Type | Description |
---|---|
Tensor
|
A torch.Tensor of shape [2, 2, ..., batch_size]. |
Example:
from torch import rand
from pyqtorch import QuantumCircuit, RY, random_state, run
n_qubits = 2
circ = QuantumCircuit(n_qubits, [RY(0, 'theta')])
state = random_state(n_qubits)
run(circ, state, {'theta': rand(1)})
Source code in pyqtorch/api.py
sample
Sample from circuit
given an input state state
given
current parameter values values
, perform an optional embedding
on values
and return a list Counter objects mapping from
Parameters:
Name | Type | Description | Default |
---|---|---|---|
circuit
|
QuantumCircuit
|
A pyqtorch.QuantumCircuit instance. |
required |
state
|
Tensor
|
A torch.Tensor of shape [2, 2, ..., batch_size]. |
None
|
values
|
dict[str, Tensor] | None
|
A dictionary containing <'parameter_name': torch.Tensor> pairs
denoting the current parameter values for each parameter in |
None
|
n_shots
|
int
|
A positive int denoting the number of requested samples. |
1000
|
embedding
|
Embedding | None
|
An optional instance of |
None
|
Returns:
Type | Description |
---|---|
list[Counter]
|
A list of Counter objects containing |
Example:
from torch import rand
from pyqtorch import random_state, sample, QuantumCircuit, RY
n_qubits = 2
circ = QuantumCircuit(n_qubits, [RY(0, 'theta')])
state = random_state(n_qubits)
sample(circ, state, {'theta': rand(1)}, n_shots=1000)[0]
Source code in pyqtorch/api.py
expectation
Compute the expectation value of circuit
given a state
,
parameter values values
and an observable
and optionally compute gradients using diff_mode.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
circuit
|
QuantumCircuit
|
A pyqtorch.QuantumCircuit instance. |
required |
state
|
Tensor
|
A torch.Tensor of shape [2, 2, ..., batch_size]. |
None
|
values
|
dict[str, Tensor] | None
|
A dictionary containing <'parameter_name': torch.Tensor> pairs
denoting the current parameter values for each parameter in |
None
|
observable
|
Observable
|
A pyq.Observable instance. |
None
|
diff_mode
|
DiffMode
|
The differentiation mode. |
AD
|
n_shots
|
int | None
|
Number of shots for estimating expectation values. Only used with DiffMode.GPSR or DiffMode.AD. |
None
|
embedding
|
Embedding | None
|
An optional instance of |
None
|
Returns:
Type | Description |
---|---|
Tensor
|
An expectation value. |
Example:
from torch import pi, ones_like, tensor
from pyqtorch import random_state, RY, expectation, DiffMode, Observable, Add, Z, QuantumCircuit
from torch.autograd import grad
n_qubits = 2
circ = QuantumCircuit(n_qubits, [RY(0, 'theta')])
state = random_state(n_qubits)
theta = tensor(pi, requires_grad=True)
observable = Observable(Add([Z(i) for i in range(n_qubits)]))
expval = expectation(circ, state, {'theta': theta}, observable, diff_mode = DiffMode.ADJOINT)
dfdtheta= grad(expval, theta, ones_like(expval))[0]
Source code in pyqtorch/api.py
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
|