qermit.utils — Qermit 0.2.0 documentation

# qermit.utils

class qermit.taskgraph.utils.SymbolsDict

  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.

  __init__()

    Default constructor, creates an empty OrderedDict() object for future symbols to be added to.

  __repr__()

    Return repr(self).

  __str__()

    Return str(self).

  __weakref__

    list of weak references to the object (if defined)

  add_symbol(symbol: Union[str, sympy.core.symbol.Symbol])

    Adds any passed Symbol (in string form or sympy Symbol type) as a key to dictionary with None value assigned.

    Parameters

      symbol (Union[str, Symbol]) – Symbol to be added to self._symbolic_map

  add_value(symbol: sympy.core.symbol.Symbol, value: float)

    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: numpy.ndarray) → Dict[sympy.core.symbol.Symbol, float]

    Assigns given values in parameters to keys in self._symbolic_map in order, for a new dictionary object. Returns just this dictionary type.

    Parameters

      symbol_values (ndarray) – Ordered values to match to ordered keys for new dict object.

    Returns

      New dict object mapping symbol to value.

    Return type

      Dict[Symbol, float]

  set_values(symbol_values: numpy.ndarray)

    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.

    Return type

      Dict[Symbol, float]

  classmethod symbols_from_circuit(circuit: pytket._tket.circuit.Circuit) → qermit.taskgraph.utils.SymbolsDict

    Given a pytket Circuit, returns a SymbolsDict object capturing given circuits free symbols.

    Parameters

      circuit (Circuit) – Pytket circuit with potential symbols.

  classmethod symbols_from_dict(symbol_dict: Dict[sympy.core.symbol.Symbol, Union[None, float]]) → qermit.taskgraph.utils.SymbolsDict

    Assigns to mit_symbols attribute _symbolic_map straight from passed dictionary of Symbol to None/float.

    Parameters

      symbol_dict (Dict[Symbol, Union[None, float]) – Dictionary from Circuit symbolics to values.

  classmethod symbols_from_list(symbols_list: Iterable[Union[sympy.core.symbol.Symbol, str]]) → qermit.taskgraph.utils.SymbolsDict

    Adds all symbols (or string representing Symbol) as dict entries with no value.

    Parameters

      symbols_list (Iterable[Union[Symbol, str]) – A list of strings representing Symbols or Symbols.

  property symbols_list: Iterable[sympy.core.symbol.Symbol]

    Returns all symbols held in dictionary of symbols in SymbolsDict object.

    Returns

      Iterable containing all keys from _symbolic_map, i.e. all Symbols

    Return type

      Iterable[Symbol]

class qermit.taskgraph.utils.MeasurementCircuit(symbolic_circuit: pytket._tket.circuit.Circuit, symbols: Optional[qermit.taskgraph.utils.SymbolsDict] = None)

  Stores a single measurement circuit that captures one or multiple observable estimations for some Ansatz Circuit.

  __init__(symbolic_circuit: pytket._tket.circuit.Circuit, symbols: Optional[qermit.taskgraph.utils.SymbolsDict] = None)

    Stores information required to instantiate any MeasurementCircuit with parameterised symbols.

    Parameters

      - symbolic_circuit (Circuit) – Measurement circuit, may or may not have symbolics.

      - symbols (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)

  property circuit: pytket._tket.circuit.Circuit

    Returns measurement circuit stored in oracle.

    Returns

      Circuit in oracle

    Return type

      Circuit

  get_parametric_circuit() → pytket._tket.circuit.Circuit

    Substitutes parameters held in SymbolDict into copy of circuit and returns.

    Returns

      Substituted circuit

    Return type

      Circuit

  property symbols: Tuple[sympy.core.symbol.Symbol, ...]

    Converts symbols_list property held in SymbolsDict to a tuple and returns it.

    Returns

      All Symbols in object

    Return type

      Tuple[List[Symbol]]

class qermit.taskgraph.utils.ObservableTracker(qubit_pauli_operator: pytket.utils.operators.QubitPauliOperator = {})

  Stores all measurement circuits required to get observable expectations for each QubitPauliString in a given QubitPauliOperator.

  __init__(qubit_pauli_operator: pytket.utils.operators.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 for which given ObservableTracker is expected to retain measurement circuits all QubitPauliString keys for before any Backend execution.

  __repr__()

    Return repr(self).

  __str__()

    Return str(self).

  __weakref__

    list of weak references to the object (if defined)

  add_measurement_circuit(circuit: qermit.taskgraph.utils.MeasurementCircuit, measurement_info: List[Tuple[pytket._tket.pauli.QubitPauliString, List[pytket._tket.circuit.Bit], bool]])

    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[MeasurementInfo] i.e. 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: pytket._tket.pauli.QubitPauliString) → bool

    Returns true if given QubitPauliString has a measurement circuit stored in self._measurement_circuits.

    Parameters

      string (QubitPauliString) – Operator measurement circuit existence being checked for.

    Returns

      True if string has measurement circuit, false if not.

    Return type

      bool

  clear()

    Erases all held information that is not the qubit pauli operator.

  extend_operator(new_operator: pytket.utils.operators.QubitPauliOperator)

    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() → qermit.taskgraph.utils.ObservableTracker

    Copies each class attribute from to_copy to self. Returns self.

    Parameters

      to_copy ('ObservableTracker') – An alternative ObservableTracker for making a copy of.

    Returns

      New ObservableTracker object

  get_empty_strings() → List[pytket._tket.pauli.QubitPauliString]

    Returns all strings in operator that don’t have some assigned MeasurementCircuit.

    Returns

      Strings that require some MeasurementCircuit to be set

    Return type

      List[QubitPauliString]

  get_expectations(results: List[pytket.backends.backendresult.BackendResult]) → pytket.utils.operators.QubitPauliOperator

    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 – Result objects to derive counts and then an expectation from.

    Returns

      Expectation for each QubitPauliString in self._qps_to_indices

    Type

      QubitPauliOperator

  get_measurement_circuits(string: pytket._tket.pauli.QubitPauliString) → List[qermit.taskgraph.utils.MeasurementCircuit]

    Returns the measurements required to be run for a single QubitPauliString’s expectation.

    Parameters

      string (QubitPauliString) – QubitPauliString of interest.

    Returns

      Measurement Circuit run to find expection of QubitPauliString for some undefined ansatz circuit.

    Return type

      MeasurementCircuit

  property measurement_circuits: List[qermit.taskgraph.utils.MeasurementCircuit]

    Returns all measurement circuits aded to ObservableTracker via get_measurement_circuit.

    Returns

      All measurement circuits held in ObservableTracker self._measurement_circuits attirbute.

    Return type

      List[MeasurementCircuit]

  modify_coefficients(new_coefficients: List[Tuple[pytket._tket.pauli.QubitPauliString, float]])

    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

    Return type

      QubitPauliOperator

  remove_strings(strings: List[pytket._tket.pauli.QubitPauliString])

    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