Measurements

from mpqp.measures import *

A measurement can be used to retrieve information about your quantum state. In mpqp, you can add it to a circuit as any other instruction (either when initializing the circuit, or using the add() circuit method). All kind of measurements are listed bellow, check them out for usage example.

However, if no measurement is added to the circuit before running it, the user will retrieve the state as a StateVector in the computational basis (see section Results).

Note

In the current version, we only allow one measurement per circuit.

The measurement

Measure is the base class for measurements. It regroups all the attributes and methods common to all types of measurements we support.

A measurement can be of two types: BasisMeasure or ExpectationMeasure, described in details bellow.

class Measure(targets=None, shots=0, label=None)[source]

Bases: Instruction, ABC

Abstract class representing the measurement of the quantum state generated by a quantum circuit.

This class is used to regroup attributes and methods shared by all different types of measures.

We distinguish two types of measures:

  • Basis measurement (measure some qubits in a specific basis, sample mode, or retrieve the StateVector when shots is equal to zero)

  • Expectation value (use of an observable, exact or sample mode)

Parameters
  • targets (Optional[list[int]]) – List of indices referring to the qubits on which the measure will be applied.

  • shots (int) – Number of times the circuit should be run, each of these times is called a shot.

  • label (Optional[str]) – Label used to identify the measure.

label

See parameter description.

shots

See parameter description.

Measuring in a basis

Choice of the basis

When we measure a quantum state, we project it in an orthonormal basis of the associated Hilbert space. By default, BasisMeasure operates in the computational basis, but you may want to measure the state in a custom basis, like it can be the case in the Bell game. For this purpose, you can use the Basis class.

On the other hand, some common basis are available for you to use: ComputationalBasis and HadamardBasis.

class Basis(basis_vectors, nb_qubits=None, symbols=None, basis_vectors_labels=None)[source]

Bases: object

Represents a basis of the Hilbert space used for measuring a qubit.

Parameters
  • basis_vectors (list[npt.NDArray[np.complex64]]) – List of vector composing the basis.

  • nb_qubits (Optional[int]) – Number of qubits associated with this basis. If not specified, it will be automatically inferred from basis_vectors’s dimensions.

  • symbols (Optional[tuple[str, str]]) –

  • basis_vectors_labels (Optional[list[str]]) –

Example

>>> custom_basis = Basis([np.array([1,0]), np.array([0,-1])], symbols=("↑", "↓"))
>>> custom_basis.pretty_print()
Basis: [
    [1, 0],
    [0, -1]
]
>>> circ = QCircuit([X(0), H(1), CNOT(1, 2), Y(2)])
>>> circ.add(BasisMeasure([0, 1, 2], basis=custom_basis, shots=10000))
>>> print(run(circ, IBMDevice.AER_SIMULATOR)) 
Result: None, IBMDevice, AER_SIMULATOR
 Counts: [0, 0, 0, 0, 0, 4936, 5064, 0]
 Probabilities: [0, 0, 0, 0, 0, 0.4936, 0.5064, 0]
 Samples:
  State: ↓↑↓, Index: 5, Count: 4936, Probability: 0.4936
  State: ↓↓↑, Index: 6, Count: 5064, Probability: 0.5064
 Error: None
binary_to_custom(state)[source]

Converts a binary string to a custom string representation. By default, it uses “0” and “1” but can be customized based on the provided \(symbols\).

Parameters

state (str) – The binary string (e.g., “01”) to be converted.

Returns

The custom string representation of the binary state.

Return type

str

Example

>>> basis = Basis([np.array([1,0]), np.array([0,-1])], symbols=("+", "-"))
>>> custom_state = basis.binary_to_custom("01")
>>> custom_state
'+-'
pretty_print()[source]

Nicer print for the basis, with human readable formatting.

to_computational()[source]

Converts the custom basis to the computational basis.

This method creates a quantum circuit with a custom gate represented by a unitary transformation and applies it to all qubits before measurement.

Returns

A quantum circuit representing the basis change circuit.

Example

>>> basis = Basis([np.array([1, 0]), np.array([0, -1])])
>>> circuit = basis.to_computational()
>>> print(circuit)
   ┌─────────┐
q: ┤ Unitary ├
   └─────────┘
basis_vectors

See parameter description.

nb_qubits

See parameter description.

class ComputationalBasis(nb_qubits=None)[source]

Bases: VariableSizeBasis

Basis representing the computational basis, also called Z-basis or canonical basis.

Parameters

nb_qubits (Optional[int]) – number of qubits to measure, if not specified, the size will be dynamic and automatically span across the whole circuit, even through dimension change of the circuit.

Examples

>>> ComputationalBasis(3).pretty_print()
Basis: [
    [1, 0, 0, 0, 0, 0, 0, 0],
    [0, 1, 0, 0, 0, 0, 0, 0],
    [0, 0, 1, 0, 0, 0, 0, 0],
    [0, 0, 0, 1, 0, 0, 0, 0],
    [0, 0, 0, 0, 1, 0, 0, 0],
    [0, 0, 0, 0, 0, 1, 0, 0],
    [0, 0, 0, 0, 0, 0, 1, 0],
    [0, 0, 0, 0, 0, 0, 0, 1]
]
>>> b = ComputationalBasis()
>>> b.set_size(2)
>>> b.pretty_print()
Basis: [
    [1, 0, 0, 0],
    [0, 1, 0, 0],
    [0, 0, 1, 0],
    [0, 0, 0, 1]
]
set_size(nb_qubits)[source]

To allow the user to use a basis without having to specify the size (because implicitly the size should be the number of qubits of the circuit), we use this method, that will only be called once the circuit’s size is definitive (i.e. at the last moment before the circuit is ran)

Parameters

nb_qubits (int) – number of qubits in the basis

to_computational()[source]

Converts the custom basis to the computational basis.

This method creates a quantum circuit with a custom gate represented by a unitary transformation and applies it to all qubits before measurement.

Returns

A quantum circuit representing the basis change circuit.

Example

>>> basis = Basis([np.array([1, 0]), np.array([0, -1])])
>>> circuit = basis.to_computational()
>>> print(circuit)
   ┌─────────┐
q: ┤ Unitary ├
   └─────────┘
class HadamardBasis(nb_qubits=None)[source]

Bases: VariableSizeBasis

Basis representing the Hadamard basis, also called X-basis or +/- basis.

Parameters

nb_qubits (Optional[int]) – number of qubits to measure, if not specified, the size will be dynamic and automatically span across the whole circuit, even through dimension change of the circuit.

Example

>>> HadamardBasis(2).pretty_print()
Basis: [
    [0.5, 0.5, 0.5, 0.5],
    [0.5, -0.5, 0.5, -0.5],
    [0.5, 0.5, -0.5, -0.5],
    [0.5, -0.5, -0.5, 0.5]
]
>>> circ = QCircuit([X(0), H(1), CNOT(1, 2), Y(2)])
>>> circ.add(BasisMeasure(basis=HadamardBasis()))
>>> print(run(circ, IBMDevice.AER_SIMULATOR)) 
Result: None, IBMDevice, AER_SIMULATOR
 Counts: [0, 261, 253, 0, 0, 244, 266, 0]
 Probabilities: [0, 0.25488, 0.24707, 0, 0, 0.23828, 0.25977, 0]
 Samples:
  State: ++-, Index: 1, Count: 261, Probability: 0.2548828
  State: +-+, Index: 2, Count: 253, Probability: 0.2470703
  State: -+-, Index: 5, Count: 244, Probability: 0.2382812
  State: --+, Index: 6, Count: 266, Probability: 0.2597656
 Error: None
set_size(nb_qubits)[source]

To allow the user to use a basis without having to specify the size (because implicitly the size should be the number of qubits of the circuit), we use this method, that will only be called once the circuit’s size is definitive (i.e. at the last moment before the circuit is ran)

Parameters

nb_qubits (int) – number of qubits in the basis

to_computational()[source]

Converts the custom basis to the computational basis.

This method creates a quantum circuit with a custom gate represented by a unitary transformation and applies it to all qubits before measurement.

Returns

A quantum circuit representing the basis change circuit.

Example

>>> basis = Basis([np.array([1, 0]), np.array([0, -1])])
>>> circuit = basis.to_computational()
>>> print(circuit)
   ┌─────────┐
q: ┤ Unitary ├
   └─────────┘
class VariableSizeBasis(nb_qubits=None, symbols=None)[source]

Bases: Basis, ABC

A variable-size basis with a dynamically adjustable size to different qubit numbers during circuit execution.

Parameters
  • nb_qubits (Optional[int]) – number of qubits in the basis. If not provided, the basis can be dynamically sized later using the \(set_size\) method.

  • symbols (Optional[tuple[str, str]]) – custom symbols for representing basis states, defaults to (“0”, “1”).

set_size(nb_qubits)[source]

To allow the user to use a basis without having to specify the size (because implicitly the size should be the number of qubits of the circuit), we use this method, that will only be called once the circuit’s size is definitive (i.e. at the last moment before the circuit is ran)

Parameters

nb_qubits (int) – number of qubits in the basis

The BasisMeasure

The BasisMeasure is used to project the state on a statevector of a given Basis and returns the corresponding eigenvalue.

class BasisMeasure(targets=None, c_targets=None, shots=1024, basis=None, label=None)[source]

Bases: Measure

Class representing a measure of one or several qubits in a specific basis.

By default, the computational basis will be used. The user can also precise a specific basis using the class Basis.

The number of shots indicates the number of time the measure is repeated. When shots is equal to 0 (by default), the simulator is used to produce exact value of the amplitudes/probabilities. If you don’t specify a target, the operation will apply to all qubits.

Parameters
  • targets (Optional[list[int]]) – List of indices referring to the qubits on which the measure will be applied. Defaults to the entire circuit for VariableSizeBasis and the first qubits matching the size of the basis for other basis.

  • c_targets (Optional[list[int]]) – List of indices referring to the classical bits on which the measure will be applied.

  • shots (int) – Number of shots to be performed basis: basis in which the qubits should be measured.

  • basis (Optional[Basis]) – Basis in which the measure is performed. Defaults to ComputationalBasis

  • label (Optional[str]) – Label used to identify the measure.

Examples

>>> c1 = QCircuit([H(0), H(1), CNOT(0,1), BasisMeasure()])
>>> c2 = QCircuit([
...     H(0),
...     H(2),
...     CNOT(0,1),
...     BasisMeasure([0, 1], shots=512, basis=HadamardBasis())
... ])
to_other_language(language=Language.QISKIT, qiskit_parameters=None)[source]

Transforms this instruction into the corresponding object in the language specified in the language arg.

By default, the instruction is translated to the corresponding one in Qiskit, since it is the interface we use to generate the OpenQASM code.

In the future, we will generate the OpenQASM code on our own, and this method will be used only for complex objects that are not tractable by OpenQASM (like hybrid structures).

Parameters
  • language (Language) – Enum representing the target language.

  • qiskit_parameters (Optional[set['Parameter']]) – We need to keep track of the parameters passed to qiskit in order not to define twice the same parameter. Defaults to set().

Returns

The corresponding instruction (gate or measure) in the target language.

basis

See parameter description.

c_targets

See parameter description.

property pre_measure

Measuring using an observable

Information about the state can be retrieved using the expectation value of this state measured by an observable. This is done using the Observable class to define your observable, and a ExpectationMeasure to perform the measure.

class ExpectationMeasure(observable, targets=None, shots=0, label=None)[source]

Bases: Measure

This measure evaluates the expectation value of the output of the circuit measured by the observable given as input.

If the targets are not sorted and contiguous, some additional swaps will be needed. This will affect the performance of your circuit if run on noisy hardware. The swaps added can be checked out in the pre_measure attribute.

Parameters
  • targets (Optional[list[int]]) – List of indices referring to the qubits on which the measure will be applied.

  • observable (Observable) – Observable used for the measure.

  • shots (int) – Number of shots to be performed.

  • label (Optional[str]) – Label used to identify the measure.

Warns

UserWarning – If the targets are not sorted and contiguous, some additional swaps will be needed. This will change the performance of your circuit is run on noisy hardware.

Example

>>> obs = Observable(np.diag([0.7, -1, 1, 1]))
>>> c = QCircuit([H(0), CNOT(0,1), ExpectationMeasure(obs, shots=10000)])
>>> run(c, ATOSDevice.MYQLM_PYLINALG).expectation_value 
0.85918
to_other_language(language=Language.QISKIT, qiskit_parameters=None)[source]

Transforms this instruction into the corresponding object in the language specified in the language arg.

By default, the instruction is translated to the corresponding one in Qiskit, since it is the interface we use to generate the OpenQASM code.

In the future, we will generate the OpenQASM code on our own, and this method will be used only for complex objects that are not tractable by OpenQASM (like hybrid structures).

Parameters
  • language (Language) – Enum representing the target language.

  • qiskit_parameters (Optional[set['Parameter']]) – We need to keep track of the parameters passed to qiskit in order not to define twice the same parameter. Defaults to set().

Returns

The corresponding instruction (gate or measure) in the target language.

Return type

None | str

observable

See parameter description.

class Observable(observable)[source]

Bases: object

Class defining an observable, used for evaluating expectation values.

An observable can be defined by using a Hermitian matrix, or using a combination of operators in a specific basis Pauli.

Parameters

observable (Matrix | PauliString) – can be either a Hermitian matrix representing the observable or PauliString representing the observable.

Raises
  • ValueError – If the input matrix is not Hermitian or does not have a square shape.

  • NumberQubitsError – If the number of qubits in the input observable does not match the number of target qubits.

Examples

>>> Observable(np.array([[1, 0], [0, -1]]))
Observable(array([[ 1.+0.j, 0.+0.j], [ 0.+0.j, -1.+0.j]], dtype=complex64))
>>> from mpqp.measures import I, X, Y, Z
>>> Observable(3 * I @ Z + 4 * X @ Y)  
Observable(array([[ 3.+0.j,  0.+0.j, 0.+0.j,  0.+4.j],
        [ 0.+0.j, -3.+0.j, 0.-4.j,  0.+0.j],
        [ 0.+0.j,  0.+4.j, 3.+0.j,  0.+0.j],
        [ 0.-4.j,  0.+0.j, 0.+0.j, -3.+0.j]],
    dtype=complex64))
>>> Observable(3 * I @ Z + 4 * X @ Y).pauli_string.sort_monomials()
3*I@Z + 4*X@Y
to_other_language(language, circuit=None)[source]

Converts the observable to the representation of another quantum programming language.

Parameters
  • language (Language) – The target programming language.

  • circuit (Optional[CirqCircuit]) – The Cirq circuit associated with the observable (required if language == Language.CIRQ).

Returns

Depends on the target language.

Return type

Union[SparsePauliOp, QLMObservable, Hermitian, CirqPauliSum, CirqPauliString]

Example

>>> obs = Observable(np.diag([0.7, -1, 1, 1]))
>>> obs_qiskit = obs.to_other_language(Language.QISKIT)
>>> obs_qiskit.to_list()  
[('II', (0.42499999701976776+0j)), ('IZ', (0.42499999701976776+0j)),
 ('ZI', (-0.5750000029802322+0j)), ('ZZ', (0.42499999701976776+0j))]
property matrix: Matrix

The matrix representation of the observable.

nb_qubits

Number of qubits of this observable.

property pauli_string: PauliString

The PauliString representation of the observable.

Pauli String

Observables can be defined using linear combinations of Pauli operators, these are called “Pauli strings”. In mpqp, a PauliString is a linear combination of PauliStringMonomial which are themselves combinations (tensor products) of PauliStringAtom. PauliString can be added, subtracted and tensored together, as well as multiplied by scalars.

class PauliString(monomials=None)[source]

Bases: object

Represents a Pauli string, a linear combination of Pauli monomials.

Note that as a user of our library, you would most likely never need to directly call this class. Instead, we advise you to build the Pauli strings you need from the atom we provide (see the example bellow).

Parameters

monomials (Optional[list['PauliStringMonomial']]) – List of Pauli monomials.

Example

>>> from mpqp.measures import I, X, Y, Z
>>> I @ Z + 2 * Y @ I + X @ Z
1*I@Z + 2*Y@I + 1*X@Z

Note

Pauli atoms are named I, X, Y, and Z. If you have conflicts with the gates of the same name, you could:

  • Rename the Pauli atoms:

from mpqp.measures import X as Pauli_X,  Y as Pauli_Y
ps = Pauli_X + Pauli_Y/2
  • Import the Pauli atoms directly from the module:

from mpqp.measures import pauli_string
ps = pauli_string.X + pauli_string.Y/2
static from_matrix(matrix)[source]

Constructs a PauliString from a matrix.

Parameters

matrix (Matrix) – Matrix from which the PauliString is generated.

Returns

Pauli string decomposition of the matrix in parameter.

Raises

ValueError – If the input matrix is not square or its dimensions are not a power of 2.

Return type

PauliString

Example

>>> PauliString.from_matrix(np.array([[0, 1], [1, 2]]))
1.0*I + 1.0*X + -1.0*Z
static from_other_language(pauli, min_dimension=1)[source]

Convert pauli objects from other quantum SDKs to PauliString.

Parameters
  • pauli (Union[SparsePauliOp, BraketObservable, TensorProduct, list[Term], Term, CirqPauliSum, CirqPauliString, list[CirqPauliString], CirqGateOperation]) – The pauli object(s) to be converted.

  • min_dimension (int) – Minimal dimension of the resulting Pauli string.

Returns

The converted PauliString. If the input is a list, the output will be a list of PauliString.

Return type

PauliString | list[PauliString]

Examples

>>> from cirq import LineQubit, PauliSum, X as Cirq_X, Y as Cirq_Y, Z as Cirq_Z
>>> a, b, c = LineQubit.range(3)
>>> cirq_ps = 0.5 * Cirq_Z(a) * 0.5 * Cirq_Y(b) + 2 * Cirq_X(c)
>>> PauliString.from_other_language(cirq_ps)
0.25*Z@Y@I + 2.0*I@I@X
>>> from braket.circuits.observables import (
...     Sum as BraketSum,
...     I as Braket_I,
...     X as Braket_X,
...     Y as Braket_Y,
...     Z as Braket_Z,
... )
>>> braket_ps = 0.25 * Braket_Z() @ Braket_Y() @ Braket_I() + 2 * Braket_I() @ Braket_I() @ Braket_X()
>>> PauliString.from_other_language(braket_ps)
0.25*Z@Y@I + 2*I@I@X
>>> from qiskit.quantum_info import SparsePauliOp
>>> qiskit_ps = SparsePauliOp(["IYZ", "XII"], coeffs=[0.25 + 0.0j, 2.0 + 0.0j])
>>> PauliString.from_other_language(qiskit_ps)
(0.25+0j)*Z@Y@I + (2+0j)*I@I@X
>>> from qat.core import Term
>>> my_qml_ps = [Term(0.25, "ZY", [0, 1]), Term(2, "X", [2])]
>>> PauliString.from_other_language(my_qml_ps)
0.25*Z@Y@I + 2*I@I@X
round(max_digits=4)[source]

Rounds the coefficients of the PauliString to a specified number of decimal.

Parameters

max_digits (int) – Number of decimal places to round the coefficients to.

Returns

The Pauli string with coefficients rounded to the specified number of decimals.

Return type

PauliString

Example

>>> from mpqp.measures import I, X, Y, Z
>>> ps = 0.6875 * I @ I + 0.1275 * I @ Z
>>> ps.round(1)
0.7*I@I + 0.1*I@Z
simplify(inplace=False)[source]

Simplifies the Pauli string by combining identical terms and removing terms with null coefficients.

Parameters

inplace (bool) – Indicates if self should be updated in addition of a new Pauli string being returned.

Returns

The simplified version of the Pauli string.

Return type

PauliString

Example

>>> from mpqp.measures import I, X, Y, Z
>>> (I @ I - 2 *I @ I + Z @ I - Z @ I).simplify()
-1*I@I
sort_monomials()[source]

Creates a new Pauli string with the same monomials but sorted in monomial alphabetical ascending order (and the coefficients are not taken into account).

Returns

The Pauli string with its monomials sorted.

Return type

PauliString

Example

>>> from mpqp.measures import I, X, Y, Z
>>> (2 * I @ Z + .5 * I @ X + X @ Y).sort_monomials()
0.5*I@X + 2*I@Z + 1*X@Y
to_dict()[source]

Converts the Pauli string to a dictionary representation with the keys being the Pauli monomials and the values the corresponding coefficients.

Returns

Mapping representing the Pauli string.

Return type

dict[str, float]

Example

>>> from mpqp.measures import I, X, Y, Z
>>> (1 * I @ Z + 2 * I @ I).to_dict()
{'II': 2, 'IZ': 1}
to_matrix()[source]

Converts the PauliString to a matrix representation.

Returns

Matrix representation of the Pauli string.

Return type

Matrix

Example

>>> from mpqp.measures import I, X, Y, Z
>>> pprint((I + Z).to_matrix())
[[2, 0],
 [0, 0]]
to_other_language(language, circuit=None)[source]

Converts the pauli string to pauli string of another quantum programming language.

Parameters
  • language (Language) – The target programming language.

  • circuit (Optional[CirqCircuit]) – The Cirq circuit associated with the pauli string (required for cirq).

Returns

Depends on the target language.

Return type

Union[SparsePauliOp, BraketSum, list[Term], Term, CirqPauliSum, CirqPauliString, list[CirqPauliString]]

Example

>>> from mpqp.measures import I, X, Y, Z
>>> ps = X @ X @ I + I @ Y @ I + I @ I @ Z
>>> print(ps.to_other_language(Language.CIRQ))
1.000*X(q(0))*X(q(1))+1.000*Y(q(1))+1.000*Z(q(2))
>>> for term in ps.to_other_language(Language.MY_QLM):
...     print(term.op, term.qbits)
XX [0, 1]
Y [1]
Z [2]
>>> print(ps.to_other_language(Language.QISKIT))
SparsePauliOp(['IXX', 'IYI', 'ZII'],
              coeffs=[1.+0.j, 1.+0.j, 1.+0.j])
>>> for tensor in ps.to_other_language(Language.BRAKET).summands:
...     print(tensor.coefficient, "".join(a.name for a in tensor.factors))
1 XXI
1 IYI
1 IIZ
property monomials: list[PauliStringMonomial]

Monomials of the PauliString.

property nb_qubits: int

Number of qubits associated with the PauliString.

class PauliStringAtom(label, matrix)[source]

Bases: PauliStringMonomial

Represents a single Pauli operator acting on a qubit in a Pauli string.

Parameters
  • label (str) – The label representing the Pauli operator.

  • matrix (npt.NDArray[np.complex64]) – The matrix representation of the Pauli operator.

Raises

RuntimeError – New atoms cannot be created, you should use the available ones.

Note

All the atoms are already initialized. Available atoms are (I, X, Y, Z).

to_matrix()[source]

Converts the PauliString to a matrix representation.

Returns

Matrix representation of the Pauli string.

Return type

npt.NDArray[np.complex64]

Example

>>> from mpqp.measures import I, X, Y, Z
>>> pprint((I + Z).to_matrix())
[[2, 0],
 [0, 0]]
to_other_language(language, circuit=None, target=None)[source]

Converts the pauli string to pauli string of another quantum programming language.

Parameters
  • language (Language) – The target programming language.

  • circuit (Optional[CirqCircuit]) – The Cirq circuit associated with the pauli string (required for cirq).

  • target (Optional[Qid]) –

Returns

Depends on the target language.

Example

>>> from mpqp.measures import I, X, Y, Z
>>> ps = X @ X @ I + I @ Y @ I + I @ I @ Z
>>> print(ps.to_other_language(Language.CIRQ))
1.000*X(q(0))*X(q(1))+1.000*Y(q(1))+1.000*Z(q(2))
>>> for term in ps.to_other_language(Language.MY_QLM):
...     print(term.op, term.qbits)
XX [0, 1]
Y [1]
Z [2]
>>> print(ps.to_other_language(Language.QISKIT))
SparsePauliOp(['IXX', 'IYI', 'ZII'],
              coeffs=[1.+0.j, 1.+0.j, 1.+0.j])
>>> for tensor in ps.to_other_language(Language.BRAKET).summands:
...     print(tensor.coefficient, "".join(a.name for a in tensor.factors))
1 XXI
1 IYI
1 IIZ
property atoms

Atoms present. (needed for upward compatibility with PauliStringMonomial)

property coef

Coefficient of the monomial.

property monomials

Monomials of the PauliString.

property nb_qubits: int

Number of qubits associated with the PauliString.

class PauliStringMonomial(coef=1, atoms=None)[source]

Bases: PauliString

Represents a monomial in a Pauli string, consisting of a coefficient and a list of PauliStringAtom objects.

Parameters
  • coef (Real | float) – The coefficient of the monomial.

  • atoms (Optional[list['PauliStringAtom']]) – The list of PauliStringAtom objects forming the monomial.

simplify(inplace=False)[source]

Simplifies the Pauli string by combining identical terms and removing terms with null coefficients.

Parameters

inplace (bool) – Indicates if self should be updated in addition of a new Pauli string being returned.

Returns

The simplified version of the Pauli string.

Example

>>> from mpqp.measures import I, X, Y, Z
>>> (I @ I - 2 *I @ I + Z @ I - Z @ I).simplify()
-1*I@I
to_matrix()[source]

Converts the PauliString to a matrix representation.

Returns

Matrix representation of the Pauli string.

Return type

Matrix

Example

>>> from mpqp.measures import I, X, Y, Z
>>> pprint((I + Z).to_matrix())
[[2, 0],
 [0, 0]]
to_other_language(language, circuit=None)[source]

Converts the pauli string to pauli string of another quantum programming language.

Parameters
  • language (Language) – The target programming language.

  • circuit (Optional[CirqCircuit]) – The Cirq circuit associated with the pauli string (required for cirq).

Returns

Depends on the target language.

Example

>>> from mpqp.measures import I, X, Y, Z
>>> ps = X @ X @ I + I @ Y @ I + I @ I @ Z
>>> print(ps.to_other_language(Language.CIRQ))
1.000*X(q(0))*X(q(1))+1.000*Y(q(1))+1.000*Z(q(2))
>>> for term in ps.to_other_language(Language.MY_QLM):
...     print(term.op, term.qbits)
XX [0, 1]
Y [1]
Z [2]
>>> print(ps.to_other_language(Language.QISKIT))
SparsePauliOp(['IXX', 'IYI', 'ZII'],
              coeffs=[1.+0.j, 1.+0.j, 1.+0.j])
>>> for tensor in ps.to_other_language(Language.BRAKET).summands:
...     print(tensor.coefficient, "".join(a.name for a in tensor.factors))
1 XXI
1 IYI
1 IIZ
atoms

The list of atoms in the monomial.

coef

Coefficient of the monomial.

property monomials: list['PauliStringMonomial']

Monomials of the PauliString.

property nb_qubits: int

Number of qubits associated with the PauliString.

I = I

Pauli-I atom representing the identity operator in a Pauli monomial or string. Matrix representation: \(\begin{pmatrix}1&0\\0&1\end{pmatrix}\)

X = X

Pauli-X atom representing the X operator in a Pauli monomial or string. Matrix representation: \(\begin{pmatrix}0&1\\1&0\end{pmatrix}\)

Y = Y

Pauli-Y atom representing the Y operator in a Pauli monomial or string. Matrix representation: \(\begin{pmatrix}0&-i\\i&0\end{pmatrix}\)

Z = Z

Pauli-Z atom representing the Z operator in a Pauli monomial or string. Matrix representation: \(\begin{pmatrix}1&0\\0&-1\end{pmatrix}\)