Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,7 @@ src/eigenkit/
│ ├── executor.py ← Executor / Compiled protocol + lazy executor registry
│ ├── gradients.py ← photonic parameter-shift + finite-difference jacobians
│ ├── reservoir.py ← photonic reservoir (fixed interferometer)
│ ├── wire.py ← JSON codec for PhotonicCircuitSpec
│ ├── torch.py ← PhotonicCircuit nn.Module (loaded lazily)
│ └── backends/
│ ├── perceval_backend.py ← Perceval SLOS executor (perceval imported lazily)
Expand Down
37 changes: 35 additions & 2 deletions docs/user-guide/photonics.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,38 @@ fails, or if the post-selection on the number of detected photons
and the live test marked `qpu` in `tests/photonics/test_quandela_cloud.py`, which you
run with `pytest -m qpu` and a token set.

## Running through the EigenOrbit Router

A photonic circuit can also reach Quandela Cloud through the
[EigenOrbit Router](https://router.eigenorbit.com), with a Router API key in place of
a Quandela token. The executor is `RouterPhotonicBackend`, from the package
`eigenorbit-router-client` (version 0.4 or later). It is used like the other two
executors, so a circuit moves from the local simulator to the Router when you replace
the executor. The photonic devices of the Router are in limited preview.

```python
from eigenorbit_router_client import RouterConfig, RouterPhotonicBackend

from eigenkit.photonics.reservoir import run_photonic_reservoir

router = RouterPhotonicBackend(RouterConfig(device="quandela/sim_slos", shots=2000))
states = run_photonic_reservoir(spec, weights, sequences, executor=router)
```

The reservoir sends all its time steps in **one** call, because the compiled circuit of
this executor has a method `run_batch(weights, rows)`. The executor sends them as one
job, or as a few jobs when the device limits the rows of a job.
`run_photonic_reservoir` calls `run_batch` once when an executor offers it, and calls
`run` for each step when it does not, as with the local `PercevalBackend`. The results are sampled, so gradients are not
available, as on Quandela Cloud.

Two functions support services that submit jobs for their users, as the Router does.
`cloud_job_body` builds the request that creates a Quandela Cloud job, with one run
for each row of inputs, and `cloud_job_counts` reads the counts of a finished job.
Both are in `eigenkit.photonics.backends.perceval_remote`, and neither opens a
connection nor needs a token. `eigenkit.photonics.wire` encodes a
`PhotonicCircuitSpec` as JSON data, and decodes it.

## How it maps to the qubit core

The photonics package has the same layers as the core, for a different model of
Expand All @@ -268,8 +300,9 @@ adapter belongs to the photonics package, not to the qubit `torch/` package. The
qubit statevector of size \(2^n\).
- **Execution on Quandela Cloud**, on a real QPU or a cloud simulator, goes through
`QuandelaCloudBackend`. The results are sampled, so there are no gradients.
- **One scalar observable is evaluated at a time, for one sample.** There is no
batching yet.
- **One scalar observable is evaluated at a time, for one sample.** `run`,
`expectation` and `PhotonicCircuit` take one sample for each call. Only
`run_photonic_reservoir` sends a batch, to an executor that offers `run_batch`.
- **Only observables that are diagonal in the Fock basis are supported.**

## Roadmap
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ source_modules = [
"eigenkit.photonics.ir",
"eigenkit.photonics.executor",
"eigenkit.photonics.gradients",
"eigenkit.photonics.wire",
]
forbidden_modules = ["perceval", "torch", "pennylane", "qiskit", "braket", "sklearn", "qulacs"]

Expand Down
201 changes: 201 additions & 0 deletions src/eigenkit/photonics/backends/perceval_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,17 @@
from __future__ import annotations

import contextlib
import json
import os
import threading
import time
import warnings

import numpy as np

from ...core.ir import resolve
from ...core.provider import CostWarning
from ..ir import BeamSplitter, PhaseShifter
from .perceval_backend import build_perceval_circuit, fock_tuple


Expand Down Expand Up @@ -79,6 +85,201 @@ def _counts_to_distribution(counts, n_modes):
return {fock_tuple(state, n_modes): count / total for state, count in counts.items()}


# Perceval dispatches some methods (for example `with_input`, and the serializer)
# through state that all instances share, so two threads that build a job at the same
# time can swap input states or corrupt an experiment. One build takes milliseconds.
_BUILD_LOCK = threading.Lock()


class _CaptureHandler:
"""A stand-in for Perceval's cloud handler that opens no connection.

It answers the platform details that the caller already holds, and keeps the
body of the job instead of sending it.
"""

def __init__(self, platform: str, platform_details: dict) -> None:
self.name = platform
self.url = ""
self.token = ""
self.proxies: dict = {}
self._platform_details = platform_details
self.body: dict | None = None

def fetch_platform_details(self) -> dict:
return self._platform_details

def create_job(self, cloud_data: dict) -> str:
self.body = cloud_data
return "not-submitted"


def _parametric_circuit(spec):
"""Build the circuit with one named Perceval parameter per angle.

Returns the circuit and the angle expressions, in the order of the parameters
`a0`, `a1`, and so on. A batch then needs one number per angle and per row,
whatever the expressions are.
"""
import perceval as pcvl

circ = pcvl.Circuit(spec.n_modes)
angles = []

def placeholder(expression):
angles.append(expression)
return pcvl.P(f"a{len(angles) - 1}")

for comp in spec.components:
if isinstance(comp, PhaseShifter):
circ.add(comp.mode, pcvl.PS(placeholder(comp.phi)))
elif isinstance(comp, BeamSplitter):
theta = placeholder(comp.theta)
circ.add(
tuple(comp.modes), pcvl.BS(theta=theta, phi_tr=placeholder(comp.phi))
)
else:
circ.add(0, pcvl.PERM(list(comp.perm)))
return circ, angles


def cloud_job_body(
spec,
weights,
inputs,
*,
platform: str,
platform_details: dict,
shots: int,
min_detected_photons: int | None = None,
job_name: str,
) -> dict:
"""Build the body of a Quandela Cloud job, without a connection or a token.

The body is the JSON that Perceval itself sends to create a job (`POST
/api/jobs`). A service that holds the token can send it later. The job samples
the circuit once for each row of `inputs`, so a batch is one job.

Several threads can call this function at the same time: the calls into Perceval
run one after another.

Args:
spec (PhotonicCircuitSpec): The circuit to run.
weights (np.ndarray): The 1-D weight vector, used for every row.
inputs (np.ndarray): One 1-D input vector, or a 2-D array with one input
vector in each row. It needs at least one row.
platform (str): The name of the Quandela Cloud platform, for example
`"sim:slos"`.
platform_details (dict): The description of the platform, as the service
returns it (`GET /api/platforms/{name}/processor`). Perceval needs it to
prepare a job.
shots (int): The number of samples asked for in each row. It is also the
limit on the shots of each row.
min_detected_photons (int | None): Keep only the shots that detect at
least this many photons. The default is the number of photons in the
input state.
job_name (str): The name of the job on Quandela Cloud.

Returns:
The body of the request, as a dictionary that `json.dumps` accepts.

Raises:
ValueError: If `weights` is not 1-D with the length that the spec declares,
if `inputs` is not one or more rows of that length, or if the platform
does not offer the `sample_count` command.
"""
import perceval as pcvl
from perceval.algorithm import Sampler

weights = np.asarray(weights, dtype=np.float64)
if weights.shape != (spec.n_weights,):
raise ValueError(
f"weights has shape {weights.shape} but the spec needs ({spec.n_weights},)"
)
rows = np.asarray(inputs, dtype=np.float64)
if rows.ndim == 1:
rows = rows[np.newaxis, :]
if rows.ndim != 2 or rows.shape[0] < 1 or rows.shape[1] != spec.n_inputs:
raise ValueError(
f"inputs has shape {np.shape(inputs)} but the spec needs ({spec.n_inputs},) "
f"or (rows, {spec.n_inputs}) with at least one row"
)

shots = int(shots)
handler = _CaptureHandler(platform, platform_details)
with _BUILD_LOCK:
processor = pcvl.RemoteProcessor(rpc_handler=handler)
circuit, angles = _parametric_circuit(spec)
processor.set_circuit(circuit)
processor.min_detected_photons_filter(
spec.n_photons if min_detected_photons is None else min_detected_photons
)
processor.with_input(pcvl.BasicState(list(spec.input_state)))
sampler = Sampler(processor, max_shots_per_call=shots)
sampler.add_iteration_list(
[
{
"circuit_params": {
f"a{index}": float(resolve(angle, weights, row))
for index, angle in enumerate(angles)
},
"max_samples": shots,
"max_shots": shots,
}
for row in rows
]
)
job = sampler.sample_count
job.name = job_name
job.execute_async(shots)
body = handler.body
# Without a native `sample_count`, Perceval asks for another command and converts
# the result itself; `cloud_job_counts` reads the service's own counts only.
if body["payload"]["command"] != "sample_count":
raise ValueError(
f"the platform {platform!r} does not offer the sample_count command"
)
return body


def cloud_job_counts(result: dict, n_modes: int) -> list[dict]:
"""Read the counts of a finished Quandela Cloud job.

Args:
result (dict): The result of the job, as the service returns it (`GET
/api/jobs/{id}/result`).
n_modes (int): The number of modes of the circuit.

Returns:
One dictionary `{photon_counts: count}` for each row of the job, in the
order of the rows. A job with a single run gives a list of one. A row
in which no shot passed the photon filter is an empty dictionary.

Raises:
ValueError: If the result holds no counts, for example because the job
failed or was cancelled.
"""
from perceval.serialization import deserialize

inner = result.get("results") if isinstance(result, dict) else None
if isinstance(inner, str):
with contextlib.suppress(ValueError):
inner = json.loads(inner)
if isinstance(inner, dict) and "results_list" in inner:
entries = inner["results_list"]
elif isinstance(inner, dict) and "results" in inner:
entries = [inner]
else:
raise ValueError(f"the job result has no counts: {str(inner)[:200]!r}")
return [
{
fock_tuple(state, n_modes): int(count)
for state, count in deserialize(entry["results"]).items()
}
for entry in entries
]


class RemoteJobHandle:
"""A handle to a Quandela Cloud job that is running.

Expand Down
16 changes: 13 additions & 3 deletions src/eigenkit/photonics/reservoir.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,10 @@ def run_photonic_reservoir(
weights (np.ndarray): The frozen weights of the reservoir.
sequences (np.ndarray): The input sequences, of shape `(B, T, F)`.
executor (Executor | None): The executor that runs each step. The default
is `PercevalBackend()`.
is `PercevalBackend()`. If its compiled circuit has a method
`run_batch(weights, rows)`, it is called once with all the steps, as
the rows of a 2-D array, and returns one distribution for each row. A
remote executor uses it to send one job instead of one job per step.
policy (Stateless | None): The policy whose `expand_inputs` turns each
timestep into the circuit's `n_inputs` values (`window * F`). The
default is `Stateless(window=1)`. Policies that carry the state from one
Expand Down Expand Up @@ -255,9 +258,16 @@ def run_photonic_reservoir(

compiled = executor.compile(spec)
flat = inputs_bt.reshape(batch * timesteps, spec.n_inputs)
if hasattr(compiled, "run_batch"):
dists = compiled.run_batch(weights, flat)
if len(dists) != len(flat):
raise RuntimeError(
f"the executor returned {len(dists)} distributions for {len(flat)} rows"
)
else:
dists = [compiled.run(weights, x) for x in flat]
rows = np.empty((batch * timesteps, n_features), dtype=np.float64)
for row, x in enumerate(flat):
dist = compiled.run(weights, x)
for row, dist in enumerate(dists):
if features == "fock":
rows[row] = fock_probability_features(dist, basis)
else:
Expand Down
Loading
Loading