qiskit-documentation/docs/api/qiskit-addon-utils/problem-generators.mdx

160 lines
9.8 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: problem_generators (latest version)
description: API reference for qiskit_addon_utils.problem_generators in the latest version of qiskit-addon-utils
in_page_toc_min_heading_level: 2
python_api_type: module
python_api_name: qiskit_addon_utils.problem_generators
---
<span id="module-qiskit_addon_utils.problem_generators" />
<span id="problem-generators-qiskit-addon-utils-problem-generators" />
# problem\_generators
`qiskit_addon_utils.problem_generators`
Utility methods for problem generation.
### generate\_xyz\_hamiltonian
<Function id="qiskit_addon_utils.problem_generators.generate_xyz_hamiltonian" github="https://github.com/Qiskit/qiskit-addon-utils/tree/main/qiskit_addon_utils/problem_generators/generate_xyz_hamiltonian.py#L56-L188" signature="generate_xyz_hamiltonian(coupling, *, coupling_constants=(1.0, 1.0, 1.0), ext_magnetic_field=(0.0, 0.0, 0.0), pauli_order_strategy=PauliOrderStrategy.ColorThenInteraction, coloring=None)">
Generate a connectivity-aware qubit operator representing a quantum XYZ-type model.
This function implements the following Hamiltonian:
$$
\hat{H} = \sum_{(j,k)\in E} (J_{x} \sigma_j^{x} \sigma_{k}^{x} +
J_{y} \sigma_j^{y} \sigma_{k}^{y} + J_{z} \sigma_j^{z} \sigma_{k}^{z}) +
\sum_{j\in V} (h_{x} \sigma_j^{x} + h_{y} \sigma_j^{y} + h_{z} \sigma_j^{z})
$$
Where G(V,E) is the graph of the provided `coupling` map.
<Admonition title="Note" type="note">
There is often a $-\frac{1}{2}$ factor included outside the summation of this equation. This factor is not applied internally, so it should be accounted for in the `coupling_constants` and `ext_magnetic_field` inputs.
</Admonition>
```python
>>> from qiskit.transpiler import CouplingMap
>>> from qiskit_addon_utils.problem_generators import generate_xyz_hamiltonian
>>> coupling_map = CouplingMap.from_line(10)
>>> hamiltonian = generate_xyz_hamiltonian(
... coupling_map,
... coupling_constants=(0.4, 0.4, 0.0),
... ext_magnetic_field=(0.0, 0.0, 0.6),
... )
>>> print(hamiltonian)
SparsePauliOp(['IIIIIIIXXI', 'IIIIIIIYYI', 'IIIIIXXIII', 'IIIIIYYIII',
'IIIXXIIIII', 'IIIYYIIIII', 'IXXIIIIIII', 'IYYIIIIIII',
'IIIIIIIIXX', 'IIIIIIIIYY', 'IIIIIIXXII', 'IIIIIIYYII',
'IIIIXXIIII', 'IIIIYYIIII', 'IIXXIIIIII', 'IIYYIIIIII',
'XXIIIIIIII', 'YYIIIIIIII', 'IIIIIIIIIZ', 'IIIIIIIIZI',
'IIIIIIIZII', 'IIIIIIZIII', 'IIIIIZIIII', 'IIIIZIIIII',
'IIIZIIIIII', 'IIZIIIIIII', 'IZIIIIIIII', 'ZIIIIIIIII'],
coeffs=[0.4+0.j, 0.4+0.j, 0.4+0.j, 0.4+0.j, 0.4+0.j, 0.4+0.j, 0.4+0.j, 0.4+0.j,
0.4+0.j, 0.4+0.j, 0.4+0.j, 0.4+0.j, 0.4+0.j, 0.4+0.j, 0.4+0.j, 0.4+0.j,
0.4+0.j, 0.4+0.j, 0.6+0.j, 0.6+0.j, 0.6+0.j, 0.6+0.j, 0.6+0.j, 0.6+0.j,
0.6+0.j, 0.6+0.j, 0.6+0.j, 0.6+0.j])
```
**Parameters**
* **coupling** ([*CouplingMap*](/api/qiskit/qiskit.transpiler.CouplingMap "(in Qiskit v1.2)") *|*[*PyGraph*](https://www.rustworkx.org/apiref/rustworkx.PyGraph.html#rustworkx.PyGraph "(in rustworkx v0.15)") *|*[*PyDiGraph*](https://www.rustworkx.org/apiref/rustworkx.PyDiGraph.html#rustworkx.PyDiGraph "(in rustworkx v0.15)")) The qubit subgraph on which to map the Hamiltonian. Directionality of graph edges will be ignored, and parallel edges will be treated as a single edge during generation of the operator.
* **coupling\_constants** ([*Sequence*](https://docs.python.org/3/library/collections.abc.html#collections.abc.Sequence "(in Python v3.13)")*\[*[*float*](https://docs.python.org/3/library/functions.html#float "(in Python v3.13)")*]*) The real-valued coupling constants, $J_i$, in each Cartesian axis.
* **ext\_magnetic\_field** ([*Sequence*](https://docs.python.org/3/library/collections.abc.html#collections.abc.Sequence "(in Python v3.13)")*\[*[*float*](https://docs.python.org/3/library/functions.html#float "(in Python v3.13)")*]*) The coefficients, $h_i$, representing a magnetic field along each Cartesian axis.
* **pauli\_order\_strategy** ([*PauliOrderStrategy*](#qiskit_addon_utils.problem_generators.PauliOrderStrategy "qiskit_addon_utils.problem_generators.generate_xyz_hamiltonian.PauliOrderStrategy")) Indicates the iteration strategy in which the Pauli terms will be generated. See [`PauliOrderStrategy`](#qiskit_addon_utils.problem_generators.PauliOrderStrategy "qiskit_addon_utils.problem_generators.PauliOrderStrategy") for more details.
* **coloring** ([*dict*](https://docs.python.org/3/library/stdtypes.html#dict "(in Python v3.13)")*\[*[*tuple*](https://docs.python.org/3/library/stdtypes.html#tuple "(in Python v3.13)")*\[*[*int*](https://docs.python.org/3/library/functions.html#int "(in Python v3.13)")*,* [*int*](https://docs.python.org/3/library/functions.html#int "(in Python v3.13)")*],* [*int*](https://docs.python.org/3/library/functions.html#int "(in Python v3.13)")*] | None*) An optional dictionary encoding the graph coloring that is used to sort the Hamiltonian terms. This dictionary maps edge labels (in the form of integer pairs) to color values (simple integers). Hamiltonian interaction terms will be added by increasing color value. Within each color, edges are sorted which does not change anything physically but results in easier to read results.
**Returns**
A qubit operator describing a quantum XYZ-type model. The `i`-th qubit in the operator corresponds to the node in index `i` on the coupling map.
**Raises**
* [**ValueError**](https://docs.python.org/3/library/exceptions.html#ValueError "(in Python v3.13)") The coupling constants must be specified by a length-3 sequence of floating point values.
* [**ValueError**](https://docs.python.org/3/library/exceptions.html#ValueError "(in Python v3.13)") The external magnetic field must be specified by a length-3 sequence of floating point values.
**Return type**
[*SparsePauliOp*](/api/qiskit/qiskit.quantum_info.SparsePauliOp "(in Qiskit v1.2)")
</Function>
### generate\_time\_evolution\_circuit
<Function id="qiskit_addon_utils.problem_generators.generate_time_evolution_circuit" github="https://github.com/Qiskit/qiskit-addon-utils/tree/main/qiskit_addon_utils/problem_generators/generate_time_evolution_circuit.py#L23-L76" signature="generate_time_evolution_circuit(operator, *, synthesis=None, time=1.0)">
Model the time evolution of an operator.
```python
>>> from qiskit.quantum_info import SparsePauliOp
>>> from qiskit.synthesis import SuzukiTrotter
>>> from qiskit.transpiler import CouplingMap
>>> from qiskit_addon_utils.problem_generators import (
... PauliOrderStrategy,
... generate_xyz_hamiltonian,
... generate_time_evolution_circuit,
... )
```
```python
>>> coupling_map = CouplingMap.from_line(6)
>>> hamiltonian = generate_xyz_hamiltonian(
... coupling_map,
... coupling_constants=(0.4, 0.4, 0.0),
... ext_magnetic_field=(0.0, 0.0, 0.6),
... pauli_order_strategy=PauliOrderStrategy.InteractionThenColorZigZag,
... )
```
```python
>>> circ = generate_time_evolution_circuit(
... hamiltonian, synthesis=SuzukiTrotter(order=2, reps=2), time=2.0
... )
>>> _ = circ.draw("mpl", fold=-1)
```
![../\_images/qiskit\_addon\_utils-problem\_generators-1.png](/images/api/qiskit-addon-utils/qiskit_addon_utils-problem_generators-1.png)
**Parameters**
* **operator** ([*SparsePauliOp*](/api/qiskit/qiskit.quantum_info.SparsePauliOp "(in Qiskit v1.2)")) The operator for which to model the time evolution.
* **synthesis** ([*EvolutionSynthesis*](/api/qiskit/qiskit.synthesis.EvolutionSynthesis "(in Qiskit v1.2)") *| None*) A synthesis strategy. If `None`, the default synthesis is the Lie-Trotter product formula with a single repetition.
* **time** ([*float*](https://docs.python.org/3/library/functions.html#float "(in Python v3.13)")) The evolution time.
**Returns**
A [`QuantumCircuit`](/api/qiskit/qiskit.circuit.QuantumCircuit "(in Qiskit v1.2)") implementing a time-evolved operator.
**Return type**
[*QuantumCircuit*](/api/qiskit/qiskit.circuit.QuantumCircuit "(in Qiskit v1.2)")
</Function>
### PauliOrderStrategy
<Class id="qiskit_addon_utils.problem_generators.PauliOrderStrategy" github="https://github.com/Qiskit/qiskit-addon-utils/tree/main/qiskit_addon_utils/problem_generators/generate_xyz_hamiltonian.py#L29-L53" signature="PauliOrderStrategy(value)" modifiers="class">
Enumeration of different Pauli-orderings.
When constructing a Hamiltonian on a colored set of edges, the generated Pauli terms can be ordered in different ways. This order of terms in the Hamiltonian is preserved during its time evolution and, thus, directly impacts the resulting quantum circuit.
#### ColorThenInteraction
<Attribute id="qiskit_addon_utils.problem_generators.PauliOrderStrategy.ColorThenInteraction" attributeValue="1">
This strategy first iterates all edges (sorted by their color value) and then the interactions (sorted as `X`, `Y`, `Z`).
</Attribute>
#### InteractionThenColor
<Attribute id="qiskit_addon_utils.problem_generators.PauliOrderStrategy.InteractionThenColor" attributeValue="2">
This strategy is the inverse to `ColorThenInteraction`. It first iterates the interactions (sorted as `X`, `Y`, `Z`) and then all edges (sorted by their color value).
</Attribute>
#### InteractionThenColorZigZag
<Attribute id="qiskit_addon_utils.problem_generators.PauliOrderStrategy.InteractionThenColorZigZag" attributeValue="3">
This strategy is similar to the `InteractionThenColor` one. However, it alternates between iterating the edges by incrementing and decrementing color values as it jumps from one interaction to the next. For example, if only `X` and `Y` interactions are included and three color values are used (`{1, 2, 3}`), this will result in the following order: `["X on 1", "X on 2", "X on 3", "Y on 3", "Y on 2", "Y on 1"]`.
</Attribute>
</Class>