graphix.parameter module

Parameter class.

Parameter object acts as a placeholder of measurement angles and allows the manipulation of the measurement pattern without specific value assignment.

class Expression[source]

Bases: ABC

Expression with parameters.

abstractmethod with_parameter(variable: Parameter, substitute: Expression | SupportsFloat) → Expression | complex[source]

Substitute a parameter with a value or expression.

abstractmethod with_parameters(assignment: Mapping[Parameter, ExpressionOrSupportsFloat]) → ExpressionOrComplex[source]

Perform parallel substitution of multiple parameters.

subs(variable: Parameter, substitute: Expression | SupportsFloat) → Expression | complex[source]

Alias for with_parameter(), following the sympy convention.

xreplace(assignment: Mapping[Parameter, ExpressionOrSupportsFloat]) → ExpressionOrComplex[source]

Alias for with_parameters(), following the sympy convention.

class ExpressionWithTrigonometry[source]

Bases: Expression, ABC

Expression that supports trigonometric functions.

abstractmethod cos() → ExpressionWithTrigonometry[source]

Return the cosine of the expression.

abstractmethod sin() → ExpressionWithTrigonometry[source]

Return the cosine of the expression.

abstractmethod exp() → ExpressionWithTrigonometry[source]

Return the exponential of the expression.

class Parameter[source]

Bases: Expression

Abstract class for substituable parameter.

exception PlaceholderOperationError[source]

Bases: ValueError

Error raised when an operation is not supported by the placeholder.

__init__() → None[source]

Instantiate the error.

class AffineExpression(a: float, x: Parameter, b: float)[source]

Bases: Expression

Affine expression.

An affine expression is of the form a*x+b where a and b are numbers and x is a parameter.

a: float
x: Parameter
b: float
offset(d: float) → AffineExpression[source]

Add d to the expression.

scale_non_null(k: float) → AffineExpression[source]

Return self scaled by k assuming k is non-zero.

Parameters:

k (float) – Scaling factor.

Returns:

A new expression scaled by k.

Return type:

AffineExpression

scale(k: float) → Expression | float[source]

Multiply the expression by k.

evaluate(value: Expression | SupportsFloat) → Expression | float[source]

Evaluate the expression at value.

with_parameter(variable: Parameter, substitute: Expression | SupportsFloat) → AffineExpression | Expression | complex[source]

Look to the documentation in the parent class.

with_parameters(assignment: Mapping[Parameter, ExpressionOrSupportsFloat]) → ExpressionOrComplex[source]

Look to the documentation in the parent class.

__init__(a: float, x: Parameter, b: float) → None
class Placeholder(name: str)[source]

Bases: AffineExpression, Parameter

Placeholder for measurement angles.

These placeholder may appear in affine expressions. Placeholders and affine expressions may be used as angles in rotation gates of Circuit class or for the measurement angle of the measurement commands. Pattern optimizations such that standardization, signal shifting and Pauli preprocessing can be applied to patterns with placeholders.

These placeholders and affine expressions do not support arbitrary computation and are not suitable for simulation. You may use Circuit.subs() or Pattern.subs() with an actual value before the computation.

__init__(name: str) → None[source]

Create a new Placeholder object.

Parameters:

name (str) – name of the parameter, used for binding values.

property name: str

Return the name of the placeholder.

check_expression_or_complex(value: object) → Expression | complex[source]

Check that the given object is of type ExpressionOrComplex and return it.

check_expression_or_float(value: object) → Expression | float[source]

Check that the given object is of type ExpressionOrFloat and return it.

with_parameter(value: Expression | float, variable: Parameter, substitute: Expression | SupportsFloat) → Expression | float[source]
with_parameter(value: _T, variable: Parameter, substitute: Expression | SupportsFloat) → _T | complex

Substitute in value.

If value is an instance of Expression, then return value.with_parameter(variable, substitute) (coerced into a complex or a float if the result is a number).

If value does not implement with_parameter, value is returned unchanged.

This function is used to apply substitution to collections where some elements are Expression and other elements are just plain numbers.

with_parameters(value: Expression | float, assignment: Mapping[Parameter, ExpressionOrSupportsFloat]) → Expression | float[source]
with_parameters(value: _T, assignment: Mapping[Parameter, ExpressionOrSupportsFloat]) → _T | Expression | complex

Substitute in parallel in value.

If value is an instance of Expression, then return value.with_parameters(assignment) (coerced into a complex if the result is a number).

If value` does not implement with_parameters, value is returned unchanged.

This function is used to apply parallel substitutions to collections where some elements are Expression and other elements are just plain numbers.

cos_sin(angle: Expression | float) → tuple[Expression | float, Expression | float][source]

Cosine and sine of a float or an expression.

exp(z: Expression | complex) → Expression | complex[source]

Exponential of a number or an expression.

class Parameterizable[source]

Bases: ABC

Abstract base class for objects that hold parameters.

This class defines the abstract methods with_parameter() for single-parameter substitution and with_parameters() for parallel substitution of multiple parameters.

It also exposes subs() and xreplace() as aliases for with_parameter() and with_parameters(), respectively, following the sympy convention.

abstractmethod with_parameter(variable: Parameter, substitute: ExpressionOrSupportsFloat) → Self[source]

Substitute a parameter with a value or expression.

abstractmethod with_parameters(assignment: Mapping[Parameter, ExpressionOrSupportsFloat]) → Self[source]

Perform parallel substitution of multiple parameters.

subs(variable: Parameter, substitute: ExpressionOrSupportsFloat) → Self[source]

Alias for with_parameter(), following the sympy convention.

xreplace(assignment: Mapping[Parameter, ExpressionOrSupportsFloat]) → Self[source]

Alias for with_parameters(), following the sympy convention.

class InplaceParameterizable[source]

Bases: Parameterizable, ABC

Abstract base class for objects that hold parameters and can be modified in place.

This class defines the abstract methods replace_parameter() for single-parameter substitution and replace_parameters() for parallel substitution of multiple parameters. By default, these methods modify the object in place, with an optional copy parameter. If copy=True, a modified copy is returned instead.

The methods with_parameter() and with_parameters() are equivalent to calling replace_parameter() and replace_parameters(), respectively, with copy=True.

abstractmethod replace_parameter(variable: Parameter, substitute: ExpressionOrSupportsFloat, *, copy: bool = False) → Self[source]

Substitute all occurrences of the given variable in measurement angles by the given value.

Parameters:
  • variable (Parameter) – The symbolic expression to be replaced within the measurement angles.

  • substitute (ExpressionOrSupportsFloat) – The value or symbolic expression to substitute in place of variable.

  • copy (bool, optional) – If True, the current object remains unchanged and a new object is returned. The default is False, meaning that changes are performed in place.

Returns:

The object with the updated measurement parameters. Equal to self if copy is False.

Return type:

Self

abstractmethod replace_parameters(assignment: Mapping[Parameter, ExpressionOrSupportsFloat], *, copy: bool = False) → Self[source]

Substitute all occurrences of the given keys in measurement angles by the given values in parallel.

Parameters:
  • assignment (Mapping[Parameter, ExpressionOrSupportsFloat]) – A dictionary-like mapping where keys are the Parameter objects to be replaced and values are the new expressions or numerical values.

  • copy (bool, optional) – If True, the current object remains unchanged and a new object is returned. The default is False, meaning that changes are performed in place.

Returns:

The object with the updated measurement parameters. Equal to self if copy is False.

Return type:

Self

with_parameter(variable: Parameter, substitute: ExpressionOrSupportsFloat) → Self[source]

Substitute a parameter with a value or expression.

with_parameters(assignment: Mapping[Parameter, ExpressionOrSupportsFloat]) → Self[source]

Perform parallel substitution of multiple parameters.