circuit
eigenkit.circuit
The top-level Circuit function, which builds a circuit for the chosen interface.
Circuit(...) returns a different type of circuit depending on interface, as
in PennyLane:
- With
interface="numpy", the default, it returns the core NumPyCircuit. This circuit does not store weights: you pass theweightsat each call. The variational optimisation tools and thechemistryandoptimizationpackages use this form. With the defaultprovider="numpy", the circuit runs on the built-in reference simulator, sopip install eigenkitwith no extra is enough to build and run circuits. - With
interface="torch", it returns aTorchCircuit. This is a PyTorchnn.Modulethat stores trainable weights and can be used inside PyTorch models. It comes fromeigenkit.torch, which needs thetorchextra. PyTorch is imported only when this interface is requested, so an environment without PyTorch never imports it.
Both interface and provider default to NumPy. To use PyTorch, pass
interface="torch". To use a faster simulator or hardware, pass a provider such
as provider="pennylane" or provider="qiskit".
Example
import numpy as np
from eigenkit import Circuit, Encoding, Variational, all_z
blocks = [Encoding(in_features=4), Variational(depth=2, rotation="RY")]
circuit = Circuit(blocks=blocks, observables=all_z(4))
weights = np.zeros(circuit.spec.n_weights)
values = circuit(weights, np.array([0.1, 0.2, 0.3, 0.4])) # one value per qubit
# A differentiable PyTorch module on PennyLane (torch and pennylane extras):
layer = Circuit(
blocks=blocks, observables=all_z(4), interface="torch", provider="pennylane"
)
Circuit(blocks, observables=None, provider='numpy', interface='numpy', init=None)
Build a circuit and compile it for a backend.
This is the entry point for the rest of the library: you give it blocks, and it
returns a circuit that you can call. The type of the circuit depends on
interface. The NumPy interface returns a core Circuit, and the PyTorch
interface returns a TorchCircuit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
blocks
|
_BlocksOrSpec
|
The circuit. It can be a list of blocks to assemble, a |
required |
observables
|
ObservableLike
|
What to measure. |
None
|
provider
|
ProviderSpec
|
The backend that runs the circuit. It can be a registered name
( |
'numpy'
|
interface
|
_Interface
|
|
'numpy'
|
init
|
Tensor | None
|
The initial weights of the PyTorch circuit, of
shape |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
circuit |
Circuit | TorchCircuit
|
The circuit for the requested |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |