constraint

Representations of constraints on qubits.

class parityos.optimization.constraint.Constraint

Bases: HasFrozenUnorderedQubits[QubitT], ABC

Interface for constraints between qubits.

abstractmethod is_satisfied(state: PauliBasisState[QubitT, Z[Never]]) → bool

Evaluate whether the constraint is satisfied by the given state.

Caution

All qubits from the constraint must be present in state.

Parameters:

state – A pauli basis state.

Returns:

Whether the state satisfies the constraint.

Raises:

ParityOSException – If not all of the given qubits are present in this state.

class parityos.optimization.constraint.ConstraintSense(*values)

Bases: Enum

The relation between the left-hand and the right-hand side of a SumConstraint.

Strict inequalities are not supported, as mixed-integer programming modelers can not represent them either.

Added in version 3.2.0.

EQUAL = '='

operator_polynomial == value.

Type:

Equality

LESS_EQUAL = '<='

operator_polynomial <= value.

Type:

Less than or equal

GREATER_EQUAL = '>='

operator_polynomial >= value.

Type:

Greater than or equal

class parityos.optimization.constraint.ConstraintT

A TypeVar representing a Constraint.

alias of TypeVar(‘ConstraintT’, bound=Constraint)

class parityos.optimization.constraint.ProductConstraint(operator_product: OperatorProduct[Z[QubitT]], parity: Parity = Parity.even)

Bases: Constraint[QubitT]

A product constraint given by a tensor product of Z Operators.

Constraints arising from the parity mapping are of this type.

Product constraints can have even or odd Parity. For a state to satisfy this constrain, the constrained qubits in the state must have this parity.

Raises:

ParityOSTypeError – If not all of the given operators are Z operators.

operator_product: OperatorProduct[Z[QubitT]]

Tensor product of Z operators acting on the constrained qubits.

parity: Parity

The parity of the constraint.

classmethod from_qubits(qubits: QubitT | Iterable[QubitT], parity: Parity = Parity.even) → Self

Construct a ProductConstraint from qubits alone.

is_satisfied(state: PauliBasisState[QubitT, Z[Never]]) → bool

Evaluate whether the constraint is satisfied by the given state.

Caution

All qubits from the constraint must be present in state.

Parameters:

state – A pauli basis state.

Returns:

Whether the state satisfies the constraint.

Raises:

ParityOSException – If not all of the given qubits are present in this state.

qubits: frozenset[QubitT]
class parityos.optimization.constraint.SumConstraint(operator_polynomial: OperatorPolynomial[Z[QubitT]], value=0.0, sense=ConstraintSense.EQUAL)

Bases: Constraint[QubitT]

A constraint given by an OperatorPolynomial of Z operators.

Sum constraints are fulfilled for a state, if the value of operator_polynomial under the state is equal to (ConstraintSense.EQUAL, the default), less than or equal to (ConstraintSense.LESS_EQUAL) or greater than or equal to (ConstraintSense.GREATER_EQUAL) value.

Note

Only sum constraints support inequalities: a product of spins can only take the values \(\pm 1\), which is fully covered by the Parity of a ProductConstraint.

Changed in version 3.2.0: Added sense for inequality constraints.

Raises:

ParityOSTypeError – If not all of the given operators are Z operators.

operator_polynomial: OperatorPolynomial[Z[QubitT]]

Linear combination of tensor products of Z operators acting on the constrained qubits.

value: float

Right-hand side value of the constraint. Defaults to 0.

sense: ConstraintSense

Relation between operator_polynomial and value. Defaults to ConstraintSense.EQUAL.

property is_equality: bool

Whether this is an equality constraint, i.e. its sense is ConstraintSense.EQUAL.

is_satisfied(state: PauliBasisState[QubitT, Z[Never]]) → bool

Evaluate whether the constraint is satisfied by the given state.

Caution

All qubits from the constraint must be present in state.

Parameters:

state – A pauli basis state.

Returns:

Whether the state satisfies the constraint.

Raises:
  • ParityOSException – If not all of the given qubits are present in this state.

  • ParityOSTypeError – If the constraint is an inequality and the value of the polynomial is not a real number (e.g. due to complex or symbolic coefficients).

qubits: frozenset[QubitT]