graphix.simulator module¶
MBQC simulator.
Simulates MBQC by executing the pattern.
- class PrepareMethod[source]¶
Bases:
ABCPrepare method used by the simulator.
See
DefaultPrepareMethodfor the default prepare method that implements MBQC.To be overwritten by custom preparation methods in the case of delegated QC protocols.
Example: class
ClientPrepareMethodin https://github.com/qat-inria/veriphix
- class DefaultPrepareMethod[source]¶
Bases:
PrepareMethodDefault prepare method implementing standard preparation for MBQC.
- class MeasureMethod[source]¶
Bases:
ABCMeasure method used by the simulator, with default measurement method that implements MBQC.
To be overwritten by custom measurement methods in the case of delegated QC protocols.
Example: class
ClientMeasureMethodin https://github.com/qat-inria/veriphix- measure(backend: Backend[_StateT_co], cmd: BaseM, noise_model: NoiseModel | None = None, rng: Generator | None = None, *, stacklevel: int = 1) None[source]¶
Perform a measurement.
- Parameters:
backend (
Backend) – The simulator backend to use.cmd (BaseM) – Measurement command.
noise_model (
NoiseModel, optional) – Noise model used to confuse result.rng (Generator, optional) – Random number generator used to confuse result.
stacklevel (int, optional) – Stack level to use for warnings. Defaults to 1, meaning that warnings are reported at this function’s call site.
- abstractmethod describe_measurement(cmd: BaseM) Measurement[source]¶
Return the description of the measurement performed by a command.
- Parameters:
cmd (BaseM) – Measurement command whose description is required.
- Returns:
Plane and angle actually used by the backend.
- Return type:
- abstractmethod measurement_outcome(node: int) Outcome[source]¶
Return the result of a previous measurement.
- class DefaultMeasureMethod(results: Mapping[int, Outcome] | None = None)[source]¶
Bases:
MeasureMethodDefault measurement method implementing standard measurement plane/angle update for MBQC.
- __init__(results: Mapping[int, Outcome] | None = None)[source]¶
Initialize with an optional result dictionary.
- Parameters:
results (Mapping[int, Outcome] | None, optional) – Mapping of previously measured nodes to their results. If
None, an empty dictionary is created.
Notes
If a mapping is provided, it is treated as read-only. Measurements performed during simulation are stored in
self.results, which is a copy of the given mapping. The originalresultsmapping is not modified.
- describe_measurement(cmd: BaseM) Measurement[source]¶
Return the description of the measurement performed by
cmd.- Parameters:
cmd (BaseM) – Measurement command whose plane and angle should be updated.
- Returns:
Updated measurement specification.
- Return type:
- class SimulatorKwargs[source]¶
Bases:
TypedDictCommon keyword arguments for simulator.
The keys correspond to the fields of
SimulatorOptions.- prepare_method: PrepareMethod | None¶
- measure_method: MeasureMethod | None¶
- noise_model: NoiseModel | None¶
- branch_selector: BranchSelector | None¶
- class SimulatorOptions(prepare_method: PrepareMethod | None = None, measure_method: MeasureMethod | None = None, noise_model: NoiseModel | None = None, branch_selector: BranchSelector | None = None, graph_prep: str | None = None, symbolic: bool = False)[source]¶
Bases:
objectOptions controlling simulator.
- Parameters:
prepare_method (
PrepareMethod, optional) – Prepare method used by the simulator. Default isDefaultPrepareMethod.measure_method (
MeasureMethod, optional) – Measure method used by the simulator. Default isDefaultMeasureMethod.noise_model (
NoiseModel, optional) – [Density matrix backend only] Noise model used by the simulator.branch_selector (
BranchSelector, optional) – Branch selector used for measurements. Can only be specified ifbackendis not an already instantiatedBackendobject. Default isRandomBranchSelector.graph_prep (str, optional) – [Tensor network backend only] Strategy for preparing the graph state. See
TensorNetworkBackend.symbolic (bool, optional) – [Density matrix backend only] If True, support arbitrary objects (typically, symbolic expressions) in measurement angles.
- prepare_method: PrepareMethod | None = None¶
- measure_method: MeasureMethod | None = None¶
- noise_model: NoiseModel | None = None¶
- branch_selector: BranchSelector | None = None¶
- __init__(prepare_method: PrepareMethod | None = None, measure_method: MeasureMethod | None = None, noise_model: NoiseModel | None = None, branch_selector: BranchSelector | None = None, graph_prep: str | None = None, symbolic: bool = False) None¶
- class PatternSimulator(pattern: Pattern, backend: Literal['statevector'] = ..., *, stacklevel: int = 1, **kwargs: Unpack[SimulatorKwargs])[source]¶
- class PatternSimulator(pattern: Pattern, backend: Literal['densitymatrix'] = ..., *, stacklevel: int = 1, **kwargs: Unpack[SimulatorKwargs])
- class PatternSimulator(pattern: Pattern, backend: Literal['tensornetwork', 'mps'] = ..., *, stacklevel: int = 1, **kwargs: Unpack[SimulatorKwargs])
- class PatternSimulator(pattern: Pattern, backend: Backend[_StateT] = ..., *, stacklevel: int = 1, **kwargs: Unpack[SimulatorKwargs])
Bases:
Generic[_StateT_co]MBQC simulator.
Executes the measurement pattern.
- __init__(pattern: Pattern, backend: Literal['statevector'] = 'statevector', *, stacklevel: int = 1, **kwargs: Unpack[SimulatorKwargs]) None[source]¶
- __init__(pattern: Pattern, backend: Literal['densitymatrix'] = 'statevector', *, stacklevel: int = 1, **kwargs: Unpack[SimulatorKwargs]) None
- __init__(pattern: Pattern, backend: Literal['tensornetwork', 'mps'] = 'statevector', *, stacklevel: int = 1, **kwargs: Unpack[SimulatorKwargs]) None
- __init__(pattern: Pattern, backend: Backend[_StateT] = 'statevector', *, stacklevel: int = 1, **kwargs: Unpack[SimulatorKwargs]) None
Construct a pattern simulator.
- Parameters:
pattern (
Patternobject) – MBQC pattern to be simulated.backend (
Backendor {‘statevector’, ‘densitymatrix’, ‘tensornetwork’}, optional) – The simulator backend to use: either an instantiated backend or the name of a built-in backend. Default:'statevector'.stacklevel (int, optional) – Stack level to use for warnings. Defaults to 1, meaning that warnings are reported at this function’s call site.
kwargs (Unpack[SimulatorKwargs]) – Options controlling simulator. See
SimulatorOptions.seealso: (..) –
graphix.sim.statevec.StatevectorBackendgraphix.sim.tensornet.TensorNetworkBackendgraphix.sim.density_matrix.DensityMatrixBackend:
- noise_model: NoiseModel | None¶
- property measure_method: MeasureMethod¶
Return the measure method.
- run(input_state: Data | None = graphix.states.PlanarState(Plane.XY, 0), rng: Generator | None = None, *, stacklevel: int = 1) None[source]¶
Perform the simulation.
- Parameters:
input_state (Data | None, optional) – the output quantum state, in the representation depending on the backend used. Default:
|+>. IfNone, no input nodes are added by the simulator to the backend: input nodes must have been prepared in the backend before running the simulation.rng (Generator, optional) – Random number generator for measurements. This generator is used only in case of random branch selection (see
RandomBranchSelector).stacklevel (int, optional) – Stack level to use for warnings. Defaults to 1, meaning that warnings are reported at this function’s call site.
- class Simulable[source]¶
Bases:
Generic[_AM_co]Base class for simulable objects.
This class is generic in the type of measurements (
_AM_co), allowing generic classes to extend it while restricting simulable instances to concrete measurement types, i.e., when the type parameter is a subtype ofMeasurement.This class should not be instantiated directly, and subclasses should implement at least one of the two methods,
to_pattern` or ``to_standardizedpattern. This class is not declared as anABC, andto_patternandto_standardizedpatternare not abstract methods, since each has a default implementation that calls the other.- to_standardizedpattern() StandardizedPattern[source]¶
Return a representation as a standardized pattern.
- to_optimized_pattern(*, stacklevel: int = 1) Pattern[source]¶
Return a representation as an optimized pattern.
Optimized pattern is the form that is simulated when
simulate()is called withoptimized=True(the default).Optimization passes are: - remove Pauli measurements, - minimize space.
Note that Pauli measurements are not implicitly inferred.
- simulate(backend: StatevectorBackend | Literal['statevector'] = 'statevector', input_state: State | Statevector | Iterable[State] | Iterable[ExpressionOrSupportsComplex] | Iterable[Iterable[ExpressionOrSupportsComplex]] | None = graphix.states.PlanarState(Plane.XY, 0), rng: Generator | None = None, *, optimized: bool = True, stacklevel: int = 1, **kwargs: Unpack[SimulatorKwargs]) Statevector[source]¶
- simulate(backend: DensityMatrixBackend | Literal['densitymatrix'], input_state: State | DensityMatrix | Iterable[State] | Iterable[ExpressionOrSupportsComplex] | Iterable[Iterable[ExpressionOrSupportsComplex]] | None = graphix.states.PlanarState(Plane.XY, 0), rng: Generator | None = None, *, optimized: bool = True, stacklevel: int = 1, **kwargs: Unpack[SimulatorKwargs]) DensityMatrix
- simulate(backend: TensorNetworkBackend | Literal['tensornetwork', 'mps'], input_state: State | Iterable[State] | Iterable[ExpressionOrSupportsComplex] | Iterable[Iterable[ExpressionOrSupportsComplex]] | None = graphix.states.PlanarState(Plane.XY, 0), rng: Generator | None = None, *, optimized: bool = True, stacklevel: int = 1, **kwargs: Unpack[SimulatorKwargs]) MBQCTensorNet
- simulate(backend: Backend[_StateT_co], input_state: Data | None = graphix.states.PlanarState(Plane.XY, 0), rng: Generator | None = None, *, optimized: bool = True, stacklevel: int = 1, **kwargs: Unpack[SimulatorKwargs]) _StateT_co
Simulate the execution of the pattern by using
graphix.simulator.PatternSimulator.- Parameters:
backend (
Backendor {‘statevector’, ‘densitymatrix’, ‘tensornetwork’}, optional) – The simulator backend to use: either an instantiated backend or the name of a built-in backend. Default:'statevector'.input_state (Data or None, optional) – the output quantum state, in a representation compatible with the selected backend. Default: the
|+>state (BasicStates.PLUS). IfNone, no input nodes are added by the simulator; input nodes must have been prepared in the backend before running the simulation.rng (Generator, optional) – Random-number generator for measurements. This generator is used only in case of random branch selection (see
RandomBranchSelector).optimized (bool, optional) – Optimize the pattern before simulation. Defaults to
True.stacklevel (int, optional) – Stack level to use for warnings. Defaults to 1, meaning that warnings are reported at this function’s call site.
kwargs (Unpack[SimulatorKwargs]) – Options controlling simulator. See
SimulatorOptions.
- Returns:
state – quantum state representation for the selected backend.
See also