graphix.simulator module

MBQC simulator.

Simulates MBQC by executing the pattern.

class PrepareMethod[source]

Bases: ABC

Prepare method used by the simulator.

See DefaultPrepareMethod for the default prepare method that implements MBQC.

To be overwritten by custom preparation methods in the case of delegated QC protocols.

Example: class ClientPrepareMethod in https://github.com/qat-inria/veriphix

abstractmethod prepare(backend: Backend[_StateT_co], cmd: BaseN, rng: Generator | None = None) → None[source]

Prepare a node.

class DefaultPrepareMethod[source]

Bases: PrepareMethod

Default prepare method implementing standard preparation for MBQC.

prepare(backend: Backend[_StateT_co], cmd: BaseN, rng: Generator | None = None) → None[source]

Prepare a node.

class MeasureMethod[source]

Bases: ABC

Measure 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 ClientMeasureMethod in 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:

Measurement

abstractmethod measurement_outcome(node: int) → Outcome[source]

Return the result of a previous measurement.

Parameters:

node (int) – Node label of the measured qubit.

Returns:

Recorded measurement outcome.

Return type:

bool

abstractmethod store_measurement_outcome(node: int, result: Outcome) → None[source]

Store the result of a previous measurement.

Parameters:
  • node (int) – Node label of the measured qubit.

  • result (bool) – Measurement outcome to store.

check_domain(domain: Iterable[int]) → bool[source]

Check that the measurement outcomes match the domain condition.

Parameters:

domain (Iterable[int]) – domain on which to compute the condition for applying conditional commands.

class DefaultMeasureMethod(results: Mapping[int, Outcome] | None = None)[source]

Bases: MeasureMethod

Default 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 original results mapping is not modified.

results: dict[int, Outcome]
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:

Measurement

measurement_outcome(node: int) → Outcome[source]

Return the result of a previous measurement.

Parameters:

node (int) – Node label of the measured qubit.

Returns:

Stored measurement outcome.

Return type:

Outcome

store_measurement_outcome(node: int, result: Outcome) → None[source]

Store the result of a previous measurement.

Parameters:
  • node (int) – Node label of the measured qubit.

  • result (bool) – Measurement outcome to store.

class SimulatorKwargs[source]

Bases: TypedDict

Common 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
graph_prep: str | None
symbolic: bool
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: object

Options controlling simulator.

Parameters:
  • prepare_method (PrepareMethod, optional) – Prepare method used by the simulator. Default is DefaultPrepareMethod.

  • measure_method (MeasureMethod, optional) – Measure method used by the simulator. Default is DefaultMeasureMethod.

  • 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 if backend is not an already instantiated Backend object. Default is RandomBranchSelector.

  • 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
graph_prep: str | None = None
symbolic: bool = False
__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:
backend: Backend[_StateT_co]
noise_model: NoiseModel | None
property pattern: Pattern

Return the pattern.

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: |+>. If None, 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 of Measurement.

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 an ABC, and to_pattern and to_standardizedpattern are not abstract methods, since each has a default implementation that calls the other.

__init__() → None[source]

Check that the instance is a valid Simulable.

to_pattern() → Pattern[source]

Return a representation as a pattern.

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 with optimized=True (the default).

Optimization passes are: - remove Pauli measurements, - minimize space.

Note that Pauli measurements are not implicitly inferred.

Parameters:

stacklevel (int, optional) – Stack level to use for warnings. Defaults to 1, meaning that warnings are reported at this function’s call site.

Returns:

Optimized pattern.

Return type:

Pattern

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 (Backend or {‘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). If None, 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: