---
title: utils (latest version)
description: API reference for qiskit_addon_slc.utils in the latest version of qiskit-addon-slc
source: https://eu-de.quantum.cloud.ibm.com/docs/en/api/qiskit-addon-slc/utils
---

# Utilities

`qiskit_addon_slc.utils`

Various utilities.

This module provides a number of utility functions. Some of these exist only temporarily to work around open issues of the Qiskit SDK. When this is the case, they are marked as such and may be removed without deprecation or further notice.

### find\_indices

`find_indices(circuit, bits_or_instruction, /)`

[GitHub](https://github.com/Qiskit/qiskit-addon-slc/tree/main/qiskit_addon_slc/utils/find_indices.py#L28-L66)

Find the qubit-indices of the provided bit indices or circuit instructions.

> **Caution**
>
> This function is not considered part of the stable API! It will get removed without warning or deprecation when the same functionality is supported natively by the Qiskit SDK. See [this issue](https://github.com/Qiskit/qiskit/issues/14558) for more details.

**Parameters**

- **circuit** ([*QuantumCircuit*](/docs/api/qiskit/qiskit.circuit.QuantumCircuit)) – the quantum circuit whose qubit indices to find.
- **bits\_or\_instruction** ([*Bit*](/docs/api/qiskit/circuit#qiskit.circuit.Bit)  *|*[*CircuitInstruction*](/docs/api/qiskit/qiskit.circuit.CircuitInstruction)  *|*[*Sequence*](https://docs.python.org/3/library/collections.abc.html#collections.abc.Sequence)*\[*[*Bit*](/docs/api/qiskit/circuit#qiskit.circuit.Bit)*]*) – the bits whose indices to find. If this is a [`CircuitInstruction`](/docs/api/qiskit/qiskit.circuit.CircuitInstruction), the qubits which this instruction acts upon are used.

**Returns**

The indices of the queried bits in the circuit’s registers. If a single bit object was provided, a single `int` is returned for its index. Otherwise the return type will be a `list[int]` whose length equals the number of provided bits.

**Raises**

[**TypeError**](https://docs.python.org/3/library/exceptions.html#TypeError) – when an unexpected type of `bits_or_instruction` gets provided.

**Return type**

[int](https://docs.python.org/3/library/functions.html#int) | [list](https://docs.python.org/3/library/stdtypes.html#list)\[[int](https://docs.python.org/3/library/functions.html#int)]

### generate\_noise\_model\_paulis

`generate_noise_model_paulis(instructions, coupling_map=None, circuit=None)`

[GitHub](https://github.com/Qiskit/qiskit-addon-slc/tree/main/qiskit_addon_slc/utils/noise_model_paulis.py#L34-L96)

Generate the 1- and 2-weight Pauli terms for each of the unique 2q layer boxes provided.

**Parameters**

- **instructions** ([*list*](https://docs.python.org/3/library/stdtypes.html#list)*\[*[*CircuitInstruction*](/docs/api/qiskit/qiskit.circuit.CircuitInstruction)*]*) – the output of [`find_unique_box_instructions()`](https://qiskit.github.io/samplomatic/api/auto/samplomatic.utils.find_unique_box_instructions.html#samplomatic.utils.find_unique_box_instructions "(in samplomatic)"). Any of the provided instructions are assumed to either consists of only measurement gates or correspond to a layer of 2-qubit gate instructions. For the former, the generated noise model will contain only single-qubit `X` errors, for the latter all 1- and 2-weight Pauli errors on the reduced coupling map will be included.
- **coupling\_map** ([*CouplingMap*](/docs/api/qiskit/qiskit.transpiler.CouplingMap) *| None*) – the coupling map of the backend on which the instructions have been laid out. If this is `None`, a 1d line of qubits is assumed.
- **circuit** ([*QuantumCircuit*](/docs/api/qiskit/qiskit.circuit.QuantumCircuit) *| None*) – the transpiled circuit which has been laid out on the provided coupling map. This may only be `None` when the `coupling_map` is also `None`.

**Returns**

A dictionary mapping the `ref` attributes of the [`InjectNoise`](https://qiskit.github.io/samplomatic/api/auto/samplomatic.InjectNoise.html#samplomatic.InjectNoise "(in samplomatic)") annotation of each unique Box to the 1- and 2-weight Pauli terms whose errors are learned for this box.

**Return type**

[dict](https://docs.python.org/3/library/stdtypes.html#dict)\[[str](https://docs.python.org/3/library/stdtypes.html#str), [*QubitSparsePauliList*](/docs/api/qiskit/qiskit.quantum_info.QubitSparsePauliList)]

### get\_extremal\_eigenvalue

`get_extremal_eigenvalue(spo, **kwargs)`

[GitHub](https://github.com/Qiskit/qiskit-addon-slc/tree/main/qiskit_addon_slc/utils/davidson.py#L26-L78)

Finds the extremal eigenvalue of the provided operator.

This converts the provided operator to a sparse matrix whose minimal eigenvalue is required.

> **Note**
>
> The current implementation is definitely not optimized in terms of performance.

**Parameters**

- **spo** ([*SparsePauliOp*](/docs/api/qiskit/qiskit.quantum_info.SparsePauliOp)) – the operator whose minimal eigenvalue to find.

- **kwargs** –

  additional keyword arguments for [`davidson1()`](https://pyscf.org/pyscf_api_docs/pyscf.lib.html#pyscf.lib.linalg_helper.davidson1). When not specified otherwise, the following defaults will be used:

  - tol: 1e-6
  - max\_cycle: 500
  - max\_space: 12
  - lindep: 1e-11
  - max\_memory: 2000

  Other values will default to PySCF’s default values.

**Returns**

A pair indicating whether the Davidson algorithm has converged and the obtained minimal eigenvalue.

**Return type**

[tuple](https://docs.python.org/3/library/stdtypes.html#tuple)\[[bool](https://docs.python.org/3/library/functions.html#bool), [float](https://docs.python.org/3/library/functions.html#float)]

### iter\_circuit

`iter_circuit(circuit, *, reverse=False, log_process=True)`

[GitHub](https://github.com/Qiskit/qiskit-addon-slc/tree/main/qiskit_addon_slc/utils/circuit_iter.py#L36-L111)

Iterates over the instructions in a circuit.

> **Note**
>
> This function recurses into [`BoxOp`](/docs/api/qiskit/qiskit.circuit.BoxOp) instructions.

> **Note**
>
> Barriers in the circuit are being ignored.

**Parameters**

- **circuit** ([*QuantumCircuit*](/docs/api/qiskit/qiskit.circuit.QuantumCircuit)) – the circuit to iterate over.
- **reverse** ([*bool*](https://docs.python.org/3/library/functions.html#bool)) – whether to iterate the circuit in reverse order.
- **log\_process** ([*bool*](https://docs.python.org/3/library/functions.html#bool)) – whether to log process.

**Yields**

Tuples of length four, consisting of the encountered circuit instruction, the [canonical qubit indices](https://qiskit.github.io/samplomatic/guides/samplex_io.html#qubit-ordering-convention) (i.e. the integer indices of the acted-upon qubits in the context of the input `circuit`), the [`InjectNoise`](https://qiskit.github.io/samplomatic/api/auto/samplomatic.InjectNoise.html#samplomatic.InjectNoise "(in samplomatic)") attributes: `modifier_ref` and `ref`. The latter two items may be `None` indicating a circuit instruction that was **not** part of an unrolled [`BoxOp`](/docs/api/qiskit/qiskit.circuit.BoxOp).

**Return type**

[*Generator*](https://docs.python.org/3/library/collections.abc.html#collections.abc.Generator)\[[tuple](https://docs.python.org/3/library/stdtypes.html#tuple)\[[*CircuitInstruction*](/docs/api/qiskit/qiskit.circuit.CircuitInstruction), [list](https://docs.python.org/3/library/stdtypes.html#list)\[[int](https://docs.python.org/3/library/functions.html#int)], [str](https://docs.python.org/3/library/stdtypes.html#str) | None, [str](https://docs.python.org/3/library/stdtypes.html#str) | None]]

### map\_modifier\_ref\_to\_ref

`map_modifier_ref_to_ref(circuit)`

[GitHub](https://github.com/Qiskit/qiskit-addon-slc/tree/main/qiskit_addon_slc/utils/annotations.py#L26-L47)

Iterate a circuit and map [`InjectNoise`](https://qiskit.github.io/samplomatic/api/auto/samplomatic.InjectNoise.html#samplomatic.InjectNoise "(in samplomatic)") annotation references.

**Parameters**

**circuit** ([*QuantumCircuit*](/docs/api/qiskit/qiskit.circuit.QuantumCircuit)) – the circuit to iterate over.

**Returns**

A dictionary mapping each `InjectNoise.modifier_ref` to its `InjectNoise.ref`.

**Return type**

[dict](https://docs.python.org/3/library/stdtypes.html#dict)\[[str](https://docs.python.org/3/library/stdtypes.html#str), [str](https://docs.python.org/3/library/stdtypes.html#str)]

### remove\_measure

`remove_measure(circuit)`

[GitHub](https://github.com/Qiskit/qiskit-addon-slc/tree/main/qiskit_addon_slc/utils/remove_measure.py#L30-L44)

Remove any [`Measure`](/docs/api/qiskit/circuit#qiskit.circuit.Measure) operations from the provided circuit.

> **Note**
>
> This function recurses into [`BoxOp`](/docs/api/qiskit/qiskit.circuit.BoxOp) instructions.

**Parameters**

**circuit** ([*QuantumCircuit*](/docs/api/qiskit/qiskit.circuit.QuantumCircuit)) – the circuit whose measurements to remove.

**Returns**

The circuit without any Measure operations.

**Return type**

[*QuantumCircuit*](/docs/api/qiskit/qiskit.circuit.QuantumCircuit)
