parityos.base

class parityos.base.constraints.EqualityConstraint(operator: Iterable[Qubit], value: int)

Represents an equality constraint of the form: \(s_1 \cdot s_2 \dots \cdot s_n = \pm 1\), where \(s_1 \dots s_n\) are spin variables.

__init__(operator: Iterable[Qubit], value: int)
Parameters:
  • operator – The operator that defines the constraint. Given as a collection of qubits, the operator is then the product of Z operators on each of the qubits.

  • value – parity of the constraint; must be +1 or -1.

classmethod from_json(constraint_data)

Initializes an EqualityConstraint object from json

Parameters:

constraint_data – The constraint in json format.

Returns:

An EqualityConstraint instance.

to_json()

Converts the EqualityConstraint object to json

Returns:

The constraint in json format.

class parityos.base.constraints.ParityConstraint(qubits: Iterable[Qubit], parity: int)

The deprecated version of the EqualityConstraint class

Represents a parity constraint, which means a condition of the form: \(s_1 \cdot s_2 \dots \cdot s_n = \pm 1\) (where s_1 dots s_n are spin variables).

Parameters:
  • qubits – a collection of qubits that make up this constraint

  • parity – parity of the constraint; must be +1 or -1.

__init__(qubits: Iterable[Qubit], parity: int)
Parameters:
  • operator – The operator that defines the constraint. Given as a collection of qubits, the operator is then the product of Z operators on each of the qubits.

  • value – parity of the constraint; must be +1 or -1.

class parityos.base.circuit.Circuit(iterable=(), /)

A sequence of Gate and/or Circuit objects.

classmethod from_json(data)

Creates a Circuit from a list of elements in json

Parameters:

data – a list of elements in json format

Returns:

a Circuit object

property qubits: Set[Qubit]
Returns:

All qubits from the elements in the circuit

remap(context: Mapping | None = None, **kwargs) Circuit

Creates a copy of the circuit where the remap has been applied to all parametrized gates in the circuit (see gates.RMixin.remap for details).

Parameters:

context – a mapping of parameter names (strings) to parameter values (number-like objects) or to new parameter names (strings).

to_json()

Converts the Container to json

Returns:

a list with the elements of the circuit in json format

parityos.base.circuit.convert_cnots_to_rzzs(circuit: Circuit) Circuit

ZZ rotations instead of CNOTs.

Replaces the standards CNOTs on the optimized circuit with an equivalent implementation based on ZZ and local rotations. The resulting circuit will contain additional subcircuits to account for the necessary Rx, Ry and Rz rotations.

Parameters:

circuit (Circuit) – a circuit containing moments with CNOT gates.

Returns:

a new circuit where all CNOTs have been replaced by ZZ and local rotations.

Return type:

Circuit

class parityos.base.gates.CNOT(control: Qubit, target: Qubit)

Represents a CNOT gate

__init__(control: Qubit, target: Qubit)
Parameters:
  • control (Qubit) – The qubit that controls whether the gate will act (if Z_control == -1) or not (if Z_control == 1).

  • target (Qubit) – The qubit that will be flipped if Z_control == -1.

class parityos.base.gates.Gate(*qubit_args: Qubit)

Base class from which all gates inherit.

The Gate subclasses are intended to store the information received from the compiler. They do not implement any methods to simulate the gates, nor do they contain any information on the (anti-) commutation relations between them or other mathematical properties. For those uses we recommend more elaborate frameworks like Qutip, Cirq or Qiskit.

__init__(*qubit_args: Qubit)

Set up the list of qubits on which the gate acts.

classmethod from_json(data) Gate

Creates a gate from a json compatible object.

Parameters:

data – gate parameters in json compatible format

Returns:

a Gate instance

make_args() Tuple[Qubit]
Returns:

the sequence of arguments that would be needed to instantiate a copy of self (does not include keyword only arguments).

property qubits: Set[Qubit]
Returns:

the set of qubits on which the gate acts (including possible control qubits).

remap(*args, **kwargs) Gate

The RMixin.remap method is used to remap the parameter name of parametrized gates to new values. For other gates (the subject of this version), a simple copy of the gate is returned.

to_json() str | int | float | bool | None | Dict[str, Any] | List[Any]
Returns:

a json compatible object with all the information about the gate.

class parityos.base.gates.Gate1(qubit: Qubit)

A Gate that acts on a single qubit.

__init__(qubit: Qubit)
Parameters:

qubit (Qubit) – qubit on which the gate acts.

class parityos.base.gates.Gate2(qubit1: Qubit, qubit2: Qubit)

A Gate that acts on two qubits.

__init__(qubit1: Qubit, qubit2: Qubit)
Parameters:

qubit2 (Qubit qubit1,) – qubits on which the gate acts.

class parityos.base.gates.Gate3(qubit1: Qubit, qubit2: Qubit, qubit3: Qubit)

A Gate that acts on three qubits.

__init__(qubit1: Qubit, qubit2: Qubit, qubit3: Qubit)
Parameters:

qubit3 (Qubit qubit1, qubit2,) – qubits on which the gate acts.

class parityos.base.gates.Gate4(qubit1: Qubit, qubit2: Qubit, qubit3: Qubit, qubit4: Qubit)

A Gate that acts on four qubits.

__init__(qubit1: Qubit, qubit2: Qubit, qubit3: Qubit, qubit4: Qubit)
Parameters:

qubit4 (Qubit qubit1, qubit2, qubit3,) – qubits on which the gate acts.

class parityos.base.gates.H(qubit: Qubit)

Represents a Hadamard gate

class parityos.base.gates.RMixin(*args: Qubit, parameter_name: str | None = None, **kwargs)

A mixin that converts a gate into a rotated gate.

Attributes:

angle: the angle for the gate rotation parameter_name: a label that identifies the parameter with which to multiply the angle when implementing it. If no parameter name is given, then the gate is considered to be a ‘Fixed’ gate. If a parameter name is given, then the gate is considered to be a ‘Parametrized’ gate. This is reflected in the name of the gate in the output generated by the to_json method.

__init__(*args: Qubit, parameter_name: str | None = None, **kwargs)
Parameters:
  • args (Qubit) – a list of Qubits on which the gate acts

  • angle (float) – the angle for the gate rotation

  • parameter_name (str) – a label that identifies the parameter with which to multiply the angle when implementing it.

make_args() tuple
Returns:

the sequence of arguments that would be needed to instantiate a copy of self.

remap(context: Mapping | None = None, **kwargs) Gate

Creates a copy of the gate with an updated parameter or parameter name.

Updates for the parameter should be provided either as a context mapping or as a keyword argument. If a keyword argument is given, its key must match the parameter_name defined on the gate, otherwise an error will be raised. If no keyword argument is given, then the parameter_name is looked up in the context. If it is found, then the parameter is remapped to the corresponding value. Otherwise, a copy of the gate is returned.

If the provided value is a string, then it is interpreted as a new parameter name and the resulting gate is again a parametrized gate. Otherwise, the value should be a number-like object (an int, a float, a numpy float or even a Sympy symbol or a Qiskit Parameter can be used). Then the returned gate is a fixed gate where the angle has been multiplied with the number-like object.

A keyword argument takes precedence over the context argument.

Parameters:

context – a mapping of parameter names (strings) to parameter values (number-like objects) or to new parameter names (strings).

Examples:

rz_gate = RZ(Qubit(1), angle=math.pi, parameter_name=’parameter’) # Create a copy of rz_gate where the parameter_name is changed to ‘theta’. rz_theta = rz_gate.remap(parameter=’theta’) rz_theta = rz_gate.remap({‘parameter’: ‘theta’, ‘other_parameter’: ‘gamma’})

# Convert rz_theta into a fixed gate with the angle divided by two: rz_fixed = rz_theta.remap(theta=0.5) rz_fixed = rz_theta.remap({‘theta’: 0.5, ‘gamma’: 2.5})

to_json() str | int | float | bool | None | Dict[str, Any] | List[Any]
Returns:

a json compatible object with all the information about the gate.

class parityos.base.gates.Rx(*args: Qubit, parameter_name: str | None = None, **kwargs)

Represents an RX gate

class parityos.base.gates.Ry(*args: Qubit, parameter_name: str | None = None, **kwargs)

Represents an RY gate

class parityos.base.gates.Rz(*args: Qubit, parameter_name: str | None = None, **kwargs)

Represents an RZ gate

class parityos.base.gates.Rzz(*args: Qubit, parameter_name: str | None = None, **kwargs)

Represents an RZZ gate

class parityos.base.gates.X(qubit: Qubit)

Represents an X gate

class parityos.base.gates.Y(qubit: Qubit)

Represents a Y gate

class parityos.base.gates.Z(qubit: Qubit)

Represents a Z gate

class parityos.base.utils.JSONLoadSaveMixin

Mixin class that adds load and save methods.

load instantiates an object by loading the JSON representation from a file. save writes a JSON representation to file.

abstract classmethod from_json(data: str | int | float | bool | None | Dict[str, Any] | List[Any])

Constructs an instance from JSON data.

Parameters:

data – a JSON-like list or dict

Returns:

an instance of the class

classmethod load(filename)

Instantiate an object using data read from a file in JSON compatible format.

Parameters:

filename (str) – Name of the file that contains the JSON data.

Returns:

an instance of the class

save(filename: str)

Save the object to file in JSON compatible format.

Parameters:

filename – Name of the file where the JSON data will be stored.

abstract to_json() str | int | float | bool | None | Dict[str, Any] | List[Any]

Converts the instance to a json-compatible builtin Python object (float, str, list or dict).

Returns:

the instance in json-serializable format

parityos.base.utils.dict_filter(mapping: Mapping, keys: Collection[Hashable]) dict

Filter a mapping on a collection of keys.

Parameters:
  • mapping – a dictionary or mapping of key:value pairs

  • keys – a collection of keys

Returns:

a filtered dictionary containing only the keys that are both in the original mapping and in the collection of keys.

parityos.base.utils.json_wrap(item) str | int | float | bool | None | Dict[str, Any] | List[Any]

Converts an item to the corresponding json representation. :param item: The item to convert to json

Unordered sequences (e.g. sets) are converted to sorted list, such that the JSON output will always be the same if the same set is provided.

Note that this is not a fully json compliant converter. E.g. None values are not translated to null, and boolean types are converted to 0 or 1 instead of ‘false’ or ‘true’.