Using Ansätze¶
Ansätze are pre-built quantum circuit patterns that serve as templates for quantum algorithms. Out HSMPO surrogate works around a provided ansatz, but, as you know from previous examples, a simple Qibo circuit works too. This is because we internally take care of constructing a proper ansatz around the provided circuit.
Available Ansätze¶
In MPSTAB, we provide a few precomputed ansätze, for example Hardware Efficient or Floquet Dynamics inspired. More remarkably, we provide two, more abstract, models: a first general wrapper, which can be used to transform any Qibo circuit into an Mpstab ansatz (CircuitAnsatz), and a more complicated ansatz, which allows to provide more information about the quantum hardware and, then, recover a compiled and transpiled ansatz (TranspiledAnsatz). This second model is particularly relevant in contexts where one wants to have control of the number of gates executed on a real quantum device.
The Ansatz Catalogue lists every ansatz with its physical description and the parameters that tune its magic and its entanglement.
CircuitAnsatz¶
Wrap any existing Qibo circuit as an ansatz:
from mpstab import HSMPO
from qibo import Circuit, gates
from mpstab.models.ansatze import CircuitAnsatz
# Create a custom circuit
circuit = Circuit(5)
for q in range(5):
circuit.add(gates.H(q))
circuit.add(gates.RY(q, theta=0.5))
circuit.add(gates.CNOT(0, 1))
# Wrap as ansatz
ansatz = CircuitAnsatz(qibo_circuit=circuit)
simulator = HSMPO(ansatz=ansatz)
Circuit-library ansätze¶
MPSTAB ships a small library of textbook circuits — QFT, QFTPhaseKernel,
Grover, QPE, QAE and TrotterIsing — useful as HSMPO/HQSMPO
benchmarks. They are written with high-level gates (CU1, SWAP,
TOFFOLI, RZZ, …) and, by default, transpiled to a native gate set on
construction, so every non-Clifford gate becomes an rz rotation that HSMPO
can consume — you never hand-decompose anything:
from mpstab import HSMPO
from mpstab.models.ansatze import QFT, TrotterIsing, Grover
# Quantum Fourier Transform on 6 qubits (transpiled by default)
ansatz = QFT(nqubits=6)
simulator = HSMPO(ansatz=ansatz)
simulator.expectation("Z" * 6)
# Trotterized transverse-field Ising evolution
ising = TrotterIsing(nqubits=8, n_steps=4, dt=0.2, J=1.0, h=0.5)
# Grover search; QPE/QAE take ``n_counting`` and add one work qubit
grover = Grover(nqubits=4)
Pass transpile=False to keep the raw high-level circuit for inspection
(HSMPO itself requires the transpiled form). These pair naturally with
HQSMPO and its cut-index split — see Engines.
Building Custom Ansätze¶
Extend the Ansatz class for custom patterns:
from mpstab import HSMPO
from mpstab.models.ansatze import Ansatz
from qibo import Circuit, gates
import numpy as np
class MyCustomAnsatz(Ansatz):
"""Custom ansatz for specific problem."""
def __init__(self, nqubits, pattern="ring"):
super().__init__(nqubits)
self.pattern = pattern
self._build_circuit()
def _build_circuit(self):
# Your circuit construction logic
self.circuit = Circuit(self.nqubits)
# Add gates based on pattern
if self.pattern == "ring":
for q in range(self.nqubits):
self.circuit.add(gates.H(q))
for q in range(self.nqubits):
next_q = (q + 1) % self.nqubits
self.circuit.add(gates.CNOT(q, next_q))
# Use with HSMPO
ansatz = MyCustomAnsatz(nqubits=5, pattern="ring")
simulator = HSMPO(ansatz=ansatz)
The ansatz partitioning¶
An HSMPO surrogate is constructed grouping all the Clifford operations in the circuit and rewriting the representation of the unitary in terms of new - global - rotations. All this procedure, that is well described in the original HSMPO paper, requires a fine partitioning of the original quantum circuit, and a reconstruction of a series of operations alternately handled by the stabilizers and the tensor network engines. Our ansätze provide a built int method to partitionate and recover all these features of the provided circuit.
Further Reading¶
Research papers on ansatz design and VQA algorithms