-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add API endpoints for run, sample and expectation to pyqtorch
- Loading branch information
1 parent
9d63f8d
commit e2b4860
Showing
8 changed files
with
197 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
`pyqtorch` exposes `run`, `sample` and `expectation` routines with the following interface: | ||
|
||
## run | ||
```python | ||
def run( | ||
circuit: QuantumCircuit, | ||
state: Tensor = None, | ||
values: dict[str, Tensor] = dict(), | ||
) -> Tensor: | ||
"""Sequentially apply each operation in `circuit.operations` to an input state `state` | ||
given current parameter values `values`, perform an optional `embedding` on `values` | ||
and return an output state. | ||
Arguments: | ||
circuit: A pyqtorch.QuantumCircuit instance. | ||
state: A torch.Tensor of shape [2, 2, ..., batch_size]. | ||
values: A dictionary containing `parameter_name`: torch.Tensor key,value pairs denoting | ||
the current parameter values for each parameter in `circuit`. | ||
Returns: | ||
A torch.Tensor of shape [2, 2, ..., batch_size] | ||
""" | ||
... | ||
``` | ||
|
||
## sample | ||
```python | ||
def sample( | ||
circuit: QuantumCircuit, | ||
state: Tensor = None, | ||
values: dict[str, Tensor] = dict(), | ||
n_shots: int = 1000, | ||
) -> list[Counter]: | ||
"""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 | ||
bitstring: num_samples. | ||
Arguments: | ||
circuit: A pyqtorch.QuantumCircuit instance. | ||
state: A torch.Tensor of shape [2, 2, ..., batch_size]. | ||
values: A dictionary containing `parameter_name`: torch.Tensor key,value pairs | ||
denoting the current parameter values for each parameter in `circuit`. | ||
n_shots: A positive int denoting the number of requested samples. | ||
Returns: | ||
A list of Counter objects containing bitstring:num_samples pairs. | ||
""" | ||
... | ||
``` | ||
|
||
## expectation | ||
|
||
```python | ||
def expectation( | ||
circuit: QuantumCircuit, | ||
state: Tensor, | ||
values: dict[str, Tensor], | ||
observable: Observable, | ||
diff_mode: DiffMode = DiffMode.AD) -> torch.Tensor: | ||
"""Compute the expectation value of `circuit` given a `state`, parameter values `values` | ||
given an `observable` and optionally compute gradients using diff_mode. | ||
Arguments: | ||
circuit: A pyqtorch.QuantumCircuit instance. | ||
state: A torch.Tensor of shape [2, 2, ..., batch_size]. | ||
values: A dictionary containing `parameter_name`: torch.Tensor key,value pairs | ||
denoting the current parameter values for each parameter in `circuit`. | ||
observable: A pyq.Observable instance. | ||
diff_mode: The differentiation mode. | ||
Returns: | ||
An expectation value. | ||
""" | ||
... | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
from __future__ import annotations | ||
|
||
from collections import Counter | ||
from logging import getLogger | ||
|
||
from torch import Tensor | ||
|
||
from pyqtorch.adjoint import AdjointExpectation | ||
from pyqtorch.analog import Observable | ||
from pyqtorch.circuit import QuantumCircuit | ||
from pyqtorch.utils import DiffMode, inner_prod | ||
|
||
logger = getLogger(__name__) | ||
|
||
|
||
def run( | ||
circuit: QuantumCircuit, | ||
state: Tensor = None, | ||
values: dict[str, Tensor] = dict(), | ||
) -> Tensor: | ||
"""Sequentially apply each operation in `circuit.operations` to an input state `state` | ||
given current parameter values `values`, perform an optional `embedding` on `values` | ||
and return an output state. | ||
Arguments: | ||
circuit: A pyqtorch.QuantumCircuit instance. | ||
state: A torch.Tensor of shape [2, 2, ..., batch_size]. | ||
values: A dictionary containing `parameter_name`: torch.Tensor key,value pairs denoting | ||
the current parameter values for each parameter in `circuit`. | ||
Returns: | ||
A torch.Tensor of shape [2, 2, ..., batch_size] | ||
""" | ||
logger.debug(f"Running circuit {circuit} on state {state} and values {values}.") | ||
return circuit.run(state, values) | ||
|
||
|
||
def sample( | ||
circuit: QuantumCircuit, | ||
state: Tensor = None, | ||
values: dict[str, Tensor] = dict(), | ||
n_shots: int = 1000, | ||
) -> list[Counter]: | ||
"""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 | ||
bitstring: num_samples. | ||
Arguments: | ||
circuit: A pyqtorch.QuantumCircuit instance. | ||
state: A torch.Tensor of shape [2, 2, ..., batch_size]. | ||
values: A dictionary containing `parameter_name`: torch.Tensor key,value pairs | ||
denoting the current parameter values for each parameter in `circuit`. | ||
n_shots: A positive int denoting the number of requested samples. | ||
Returns: | ||
A list of Counter objects containing bitstring:num_samples pairs. | ||
""" | ||
logger.debug( | ||
f"Sampling circuit {circuit} on state {state} and values {values} with n_shots {n_shots}." | ||
) | ||
return circuit.sample(state, values, n_shots) | ||
|
||
|
||
def expectation( | ||
circuit: QuantumCircuit, | ||
state: Tensor, | ||
values: dict[str, Tensor], | ||
observable: Observable, | ||
diff_mode: DiffMode = DiffMode.AD, | ||
) -> Tensor: | ||
"""Compute the expectation value of `circuit` given a `state`, parameter values `values` | ||
given an `observable` and optionally compute gradients using diff_mode. | ||
Arguments: | ||
circuit: A pyqtorch.QuantumCircuit instance. | ||
state: A torch.Tensor of shape [2, 2, ..., batch_size]. | ||
values: A dictionary containing `parameter_name`: torch.Tensor key,value pairs | ||
denoting the current parameter values for each parameter in `circuit`. | ||
observable: A pyq.Observable instance. | ||
diff_mode: The differentiation mode. | ||
Returns: | ||
An expectation value. | ||
""" | ||
logger.debug( | ||
f"Computing expectation of circuit {circuit} on state {state}, values {values},\ | ||
given observable {observable} and diff_mode {diff_mode}." | ||
) | ||
if observable is None: | ||
logger.error("Please provide an observable to compute expectation.") | ||
if state is None: | ||
state = circuit.init_state(batch_size=1) | ||
if diff_mode == DiffMode.AD: | ||
state = circuit.run(state, values) | ||
return inner_prod(state, observable.run(state, values)).real | ||
elif diff_mode == DiffMode.ADJOINT: | ||
return AdjointExpectation.apply( | ||
circuit, observable, state, values.keys(), *values.values() | ||
) | ||
elif diff_mode == DiffMode.GPSR: | ||
raise NotImplementedError("To be added.") | ||
else: | ||
logger.error(f"Requested diff_mode '{diff_mode}' not supported.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters