qermit.utils¶
- class qermit.taskgraph.utils.SymbolsDict[source]¶
A helper class for standardising interfacing with Circuit Symbolics in qermit. Methods take different containers that hold some kind of symbols representation and return a SymbolsDict object. Methods access self._symbolic_map or use other accessors to modify or add new symbols.
Default constructor, creates an empty OrderedDict() object for future symbols to be added to.
- __init__()[source]¶
Default constructor, creates an empty OrderedDict() object for future symbols to be added to.
- __weakref__¶
list of weak references to the object (if defined)
- add_symbol(symbol)[source]¶
Adds any passed Symbol (in string form or sympy Symbol type) as a key to dictionary with None value assigned.
- add_value(symbol, value)[source]¶
Assigns value to self._symbolic_map[symbol]. If symbol not in object then throws an error.
- Parameters:
symbol (
Symbol
) – Symbol to have value assigned.value (
float
) – Value to assign to symbol.
- get_symbolic_map(symbol_values)[source]¶
Assigns given values in parameters to keys in self._symbolic_map in order, for a new dictionary object. Returns just this dictionary type.
- set_values(symbol_values)[source]¶
Assigns given values in parameters to keys in self._symbolic_map in order, for a new dictionary object.
- Parameters:
symbol_values (
ndarray
) – Orderd values to match to ordered keys for new dict object.- Returns:
New dict object mapping symbol to value.
- classmethod symbols_from_circuit(circuit)[source]¶
Given a pytket Circuit, returns a SymbolsDict object capturing given circuits free symbols.
- Parameters:
circuit (
Circuit
) – Pytket circuit with potential symbols.- Return type:
- classmethod symbols_from_dict(symbol_dict)[source]¶
Assigns to mit_symbols attribute _symbolic_map straight from passed dictionary of Symbol to None/float.
- class qermit.taskgraph.utils.MeasurementCircuit(symbolic_circuit, symbols=None)[source]¶
Stores a single measurement circuit that captures one or multiple observable estimations for some Ansatz Circuit.
Stores information required to instantiate any MeasurementCircuit with parameterised symbols.
- Parameters:
symbolic_circuit (
Circuit
) – Measurement circuit, may or may not have symbolics.symbols (
Optional
[SymbolsDict
]) – SymbolsDict object holding symbols and values for all symbols in Circuit. Default none if circuit not symbolic.
- __init__(symbolic_circuit, symbols=None)[source]¶
Stores information required to instantiate any MeasurementCircuit with parameterised symbols.
- Parameters:
symbolic_circuit (
Circuit
) – Measurement circuit, may or may not have symbolics.symbols (
Optional
[SymbolsDict
]) – SymbolsDict object holding symbols and values for all symbols in Circuit. Default none if circuit not symbolic.
- __weakref__¶
list of weak references to the object (if defined)
- class qermit.taskgraph.utils.ObservableTracker(qubit_pauli_operator={})[source]¶
Stores all measurement circuits required to get observable expectations for each QubitPauliString in a given QubitPauliOperator.
Default constructor, creates an empty dict object for mapping QubitPauliStrings to measurement circuits and the qubits measured to get expectation, along with an empty list for storing measurement circuits and a list for storing partitions.
- Parameters:
qubit_pauli_operator (
QubitPauliOperator
) – QubitPauliOperator for which given ObservableTracker is expected to retain measurement circuits all QubitPauliString keys for before any Backend execution.
- __init__(qubit_pauli_operator={})[source]¶
Default constructor, creates an empty dict object for mapping QubitPauliStrings to measurement circuits and the qubits measured to get expectation, along with an empty list for storing measurement circuits and a list for storing partitions.
- Parameters:
qubit_pauli_operator (
QubitPauliOperator
) – QubitPauliOperator for which given ObservableTracker is expected to retain measurement circuits all QubitPauliString keys for before any Backend execution.
- __weakref__¶
list of weak references to the object (if defined)
- add_measurement_circuit(circuit, measurement_info)[source]¶
Adds given measurement circuit to stored _measurement_circuits attribute and for each qubit pauli string and qubits in associated strings, updates dictionary between string and its measurement circuit + bit to measure and whether result should be inverted.
- Parameters:
circuit (
MeasurementCircuit
) – Measurement circuit to run to get results.measurement_info (
List
[Tuple
[QubitPauliString
,List
[Bit
],bool
]]) – Each entry contains a QubitPauliString, the bits required to take expectation over in resulting result and a bool signifying whether expectation should be inverted when taking result.
- check_string(string)[source]¶
Returns true if given QubitPauliString has a measurement circuit stored in self._measurement_circuits.
- Parameters:
string (
QubitPauliString
) – Operator measurement circuit existence being checked for.- Return type:
- Returns:
True if string has measurement circuit, false if not.
- extend_operator(new_operator)[source]¶
Extends self._qubit_pauli_operator to include tuples in passed operator.
- Parameters:
new_operator (
QubitPauliOperator
) – Each QubitPauliString and coefficient added to held operator.
- from_ObservableTracker()[source]¶
Copies each class attribute from to_copy to self. Returns self.
- Parameters:
to_copy (
ObservableTracker
) – An alternative ObservableTracker for making a copy of.- Return type:
- Returns:
New ObservableTracker object
- get_empty_strings()[source]¶
Returns all strings in operator that don’t have some assigned MeasurementCircuit.
- Return type:
- Returns:
Strings that require some MeasurementCircuit to be set
- get_expectations(results)[source]¶
For given list of results, returns a QubitPauliOperator giving an expectation for each QubitPauliString held in self._qps_to_indices. Expectation derived by taking parity of counts.
- Parameters:
results (
List
[BackendResult
]) – Result objects to derive counts and then an expectation from.- Return type:
- Returns:
Expectation for each QubitPauliString in self._qps_to_indices
- get_measurement_circuits(string)[source]¶
Returns the measurements required to be run for a single QubitPauliString’s expectation.
- Parameters:
string (
QubitPauliString
) – QubitPauliString of interest.- Return type:
- Returns:
Measurement Circuit run to find expection of QubitPauliString for some undefined ansatz circuit.
- property measurement_circuits: List[MeasurementCircuit]¶
Returns all measurement circuits aded to ObservableTracker via get_measurement_circuit.
- Returns:
All measurement circuits held in ObservableTracker self._measurement_circuits attirbute.
- modify_coefficients(new_coefficients)[source]¶
Updates coefficients in held QubitPauliOperator with new coefficients. Each QubitPauliString must already be in self._qubit_pauli_operator
- Parameters:
new_coefficients (
List
[Tuple
[QubitPauliString
,float
]]) – Each Tuple contains a QubitPauliString a new coefficient.
- property qubit_pauli_operator¶
Returns stored qubit pauli operator
- Returns:
QubitPauliOperator object stored in class
- remove_strings(strings)[source]¶
Removes passed qubit pauli strings from held QubitPauliOperator and dict from string to index.
- Parameters:
strings (
List
[QubitPauliString
]) – Qubit Pauli Strings no longer required to be measured by ObservableTracker