Circuits
A circuit is an ordered list of gates applied to an initial state.
Abstract base classes
AbstractPCircuitclassAbstractPCircuit()Abstract base class for probabilistic circuits.
sample_multiplemethodsample_multiple(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
n_samples: int = 1,
return_aux: bool = False,
) -> PyTree[Array] | tuple[PyTree[Array], PyTree[Array]]Draw n_samples samples by vmap-ing sample.
Arguments:
key: PRNG key, splitn_samplesways.inputs: Per-port pytree inputs, as insample.params: Parameter pytree for this factor.info: Runtime auxiliary info, as insample.site_info: Static per-site metadata, as insample.n_samples: Number of independent samples to draw.return_aux: Whether to return theauxpytree, as insample.
Returns:
The stacked outputs, or (outputs, auxes) when return_aux=True, each with a leading axis of size n_samples.
init_paramsmethodinit_params(key: Key[Array, '']) -> list[PyTree[Array]]Default per-gate parameters, aligned with the circuit's gates.
Splits key and calls each gate's init_params.
Arguments:
key: PRNG key.
Returns:
A list of per-gate parameter pytrees, one per gate in circuit order.
samplemethodsample(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: DFGInfo | None = None,
site_info: Any = None,
return_aux: bool = False,
) -> PyTree[Array] | tuple[PyTree[Array], tuple[PyTree[Array], ...]]Run the DAG once and return the output_name value.
Samples each site in topological order, routing parent outputs through its porting_fn.
sample_with_referencesmethodsample_with_references(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: DFGInfo | None = None,
site_info: Any = None,
n_references: int = 1,
) -> tuple[PyTree[Array], tuple[PyTree[Array], ...]]Like sample, but draws each site via its factor's sample_with_references, so every site yields n_references + 1 samples. Returns (output, per_site_auxes).
distribute_paramsmethoddistribute_params(params: PyTree[Array]) -> tuple[PyTree[Array], ...]Scatter the shared params mapping into a per-site tuple.
gather_param_gradsmethodgather_param_grads(
params: PyTree[Array],
site_grads: Mapping[int, PyTree[Array]],
) -> PyTree[Array]Gather per-site parameter gradients into the shared params dict.
distribute_infomethoddistribute_info(info: DFGInfo | None) -> tuple[PyTree, ...]Scatter info.entries into a per-site tuple.
Concrete classes
DiscretePCircuitclassDiscretePCircuit(gates: list[AbstractDiscreteGate], reps: int = 1)Probabilistic circuit for discrete gates.
Initialize a probabilistic circuit.
Arguments:
gates: the list of gates to be applied in the circuitreps: the number of times the circuit is applied to the initial input
gatesattributegates: list[AbstractDiscreteGate]num_pditsattributenum_pdits: intdimsattributedims: tuple[int, ...]repsattributereps: intsitesattributesites: tuple[Site, ...]input_portsattributeinput_ports: Mapping[str, jaxPyTree[jax.ShapeDtypeStruct]]output_specattributeoutput_spec: jaxPyTree[jax.ShapeDtypeStruct]output_nameattributeoutput_name: strtopological_orderattributetopological_order: tuple[int, ...]sites_by_nameattributesites_by_name: Mapping[str, int]sample_multiplemethodsample_multiple(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
n_samples: int = 1,
return_aux: bool = False,
) -> PyTree[Array] | tuple[PyTree[Array], PyTree[Array]]Draw n_samples samples by vmap-ing sample.
Arguments:
key: PRNG key, splitn_samplesways.inputs: Per-port pytree inputs, as insample.params: Parameter pytree for this factor.info: Runtime auxiliary info, as insample.site_info: Static per-site metadata, as insample.n_samples: Number of independent samples to draw.return_aux: Whether to return theauxpytree, as insample.
Returns:
The stacked outputs, or (outputs, auxes) when return_aux=True, each with a leading axis of size n_samples.
init_paramsmethodinit_params(key: Key[Array, '']) -> list[PyTree[Array]]Default per-gate parameters, aligned with the circuit's gates.
Splits key and calls each gate's init_params.
Arguments:
key: PRNG key.
Returns:
A list of per-gate parameter pytrees, one per gate in circuit order.
samplemethodsample(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: DFGInfo | None = None,
site_info: Any = None,
return_aux: bool = False,
) -> PyTree[Array] | tuple[PyTree[Array], tuple[PyTree[Array], ...]]Run the DAG once and return the output_name value.
Samples each site in topological order, routing parent outputs through its porting_fn.
sample_with_referencesmethodsample_with_references(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: DFGInfo | None = None,
site_info: Any = None,
n_references: int = 1,
) -> tuple[PyTree[Array], tuple[PyTree[Array], ...]]Like sample, but draws each site via its factor's sample_with_references, so every site yields n_references + 1 samples. Returns (output, per_site_auxes).
distribute_paramsmethoddistribute_params(params: PyTree[Array]) -> tuple[PyTree[Array], ...]Scatter the shared params mapping into a per-site tuple.
gather_param_gradsmethodgather_param_grads(
params: PyTree[Array],
site_grads: Mapping[int, PyTree[Array]],
) -> PyTree[Array]Gather per-site parameter gradients into the shared params dict.
distribute_infomethoddistribute_info(info: DFGInfo | None) -> tuple[PyTree, ...]Scatter info.entries into a per-site tuple.
HybridStateclassHybridState(*args, **kwargs)Discrete and continuous registers of a hybrid circuit.
discreteattributediscrete: <class 'jaxInt[Array, '... num_discrete_sites']'>continuousattributecontinuous: <class 'jaxFloat[Array, '... continuous_dim']'>HybridPCircuitclassHybridPCircuit(gates: list[AbstractDiscreteGate | AbstractHybridGate], reps: int = 1)Circuit supporting discrete, continuous, and hybrid gates.
Stores both discrete and continuous dimensions separately.
Initialize a hybrid circuit.
Arguments:
gates: List of gates (discrete, continuous, or hybrid).reps: Number of times the circuit is repeated.
gatesattributegates: list[AbstractDiscreteGate | AbstractHybridGate]discrete_dimsattributediscrete_dims: tuple[int, ...]continuous_dimsattributecontinuous_dims: tuple[int, ...]repsattributereps: intsitesattributesites: tuple[Site, ...]input_portsattributeinput_ports: Mapping[str, jaxPyTree[jax.ShapeDtypeStruct]]output_specattributeoutput_spec: jaxPyTree[jax.ShapeDtypeStruct]output_nameattributeoutput_name: strtopological_orderattributetopological_order: tuple[int, ...]sites_by_nameattributesites_by_name: Mapping[str, int]sample_multiplemethodsample_multiple(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: PyTree = None,
site_info: Any = None,
n_samples: int = 1,
return_aux: bool = False,
) -> PyTree[Array] | tuple[PyTree[Array], PyTree[Array]]Draw n_samples samples by vmap-ing sample.
Arguments:
key: PRNG key, splitn_samplesways.inputs: Per-port pytree inputs, as insample.params: Parameter pytree for this factor.info: Runtime auxiliary info, as insample.site_info: Static per-site metadata, as insample.n_samples: Number of independent samples to draw.return_aux: Whether to return theauxpytree, as insample.
Returns:
The stacked outputs, or (outputs, auxes) when return_aux=True, each with a leading axis of size n_samples.
init_paramsmethodinit_params(key: Key[Array, '']) -> list[PyTree[Array]]Default per-gate parameters, aligned with the circuit's gates.
Splits key and calls each gate's init_params.
Arguments:
key: PRNG key.
Returns:
A list of per-gate parameter pytrees, one per gate in circuit order.
samplemethodsample(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: DFGInfo | None = None,
site_info: Any = None,
return_aux: bool = False,
) -> PyTree[Array] | tuple[PyTree[Array], tuple[PyTree[Array], ...]]Run the DAG once and return the output_name value.
Samples each site in topological order, routing parent outputs through its porting_fn.
sample_with_referencesmethodsample_with_references(
key: Key[Array, ''],
inputs: Mapping[str, PyTree[Array]],
params: PyTree[Array],
info: DFGInfo | None = None,
site_info: Any = None,
n_references: int = 1,
) -> tuple[PyTree[Array], tuple[PyTree[Array], ...]]Like sample, but draws each site via its factor's sample_with_references, so every site yields n_references + 1 samples. Returns (output, per_site_auxes).
distribute_paramsmethoddistribute_params(params: PyTree[Array]) -> tuple[PyTree[Array], ...]Scatter the shared params mapping into a per-site tuple.
gather_param_gradsmethodgather_param_grads(
params: PyTree[Array],
site_grads: Mapping[int, PyTree[Array]],
) -> PyTree[Array]Gather per-site parameter gradients into the shared params dict.
distribute_infomethoddistribute_info(info: DFGInfo | None) -> tuple[PyTree, ...]Scatter info.entries into a per-site tuple.
num_discrete_sitespropertynum_discrete_sitesNumber of discrete sites in the circuit.
num_continuous_sitespropertynum_continuous_sitesNumber of continuous sites in the circuit.
continuous_state_dimpropertycontinuous_state_dimTotal dimension of continuous state = sum(continuous_dims).
continuous_offsetspropertycontinuous_offsetsPer continuous site (site, start, stop) slice into the register.