Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

qamomile.optimization.qrao

Quantum Random Access Optimization (QRAO) module.

This module provides QRAC-based encoders and converters for quantum optimization.

Overview

ClassDescription
BaseQRACEncoderAbstract base for all QRAC encoders.
GraphColoringQRACEncoderBase for graph-coloring-based QRAC encoders (21, 31, 32).
QRAC21ConverterQRAC(2,1,p) Converter for Quantum Random Access Optimization.
QRAC21Encoder(2,1,p)-QRAC Encoder.
QRAC31ConverterQRAC(3,1,p) Converter for Quantum Random Access Optimization.
QRAC31Encoder(3,1,p)-QRAC Encoder.
QRAC32ConverterQRAC(3,2,p) Converter for Quantum Random Access Optimization.
QRAC32Encoder(3,2,p)-QRAC Encoder.
QRACSpaceEfficientConverterSpace Efficient QRAC Converter for Quantum Random Access Optimization.
QRACSpaceEfficientEncoderSpace Efficient QRAC Encoder.
SignRounderSimple sign-based rounding.

Classes

BaseQRACEncoder [source]

class BaseQRACEncoder(abc.ABC)

Abstract base for all QRAC encoders.

Provides:

Subclasses must implement _perform_encoding and num_qubits.

Constructor

def __init__(self, spin_model: BinaryModel) -> None

Attributes

Methods

get_pauli_for_variable
def get_pauli_for_variable(self, var_idx: int) -> tuple[int, PauliType]

Get Pauli operator for a variable.

Parameters:

NameTypeDescription
var_idxintVariable index

Returns:

tuple[int, PauliType] — Tuple of (qubit_index, pauli_type)


GraphColoringQRACEncoder [source]

class GraphColoringQRACEncoder(BaseQRACEncoder, abc.ABC)

Base for graph-coloring-based QRAC encoders (21, 31, 32).

Subclasses only need to set max_color_group_size and paulis.

The encoding process:

  1. Graph coloring on the interaction graph.

  2. Adds linear-only variables to color groups.

  3. Assigns Pauli operators to variables within each qubit.

Constructor

def __init__(self, spin_model: BinaryModel) -> None

Attributes

Methods

encode_ising
def encode_ising(
    self,
    ising: BinaryModel,
) -> tuple[qm_o.Hamiltonian, dict[int, qm_o.PauliOperator]]

Encode a spin model into a relaxed QRAC Hamiltonian.

Uses the encoding determined by _perform_encoding and delegates operator/scale computation to _get_operator_and_scale.

Parameters:

NameTypeDescription
isingBinaryModelBinaryModel in SPIN vartype.

Returns:

tuple[qm_o.Hamiltonian, dict[int, qm_o.PauliOperator]] — Tuple of (relaxed Hamiltonian, encoding map).


QRAC21Converter [source]

class QRAC21Converter(QRACConverterBase[QRAC21Encoder])

QRAC(2,1,p) Converter for Quantum Random Access Optimization.

Converts optimization problems into QRAC-encoded form with reduced qubit count. Up to 2 variables are encoded into a single qubit using X and Z Pauli operators.

Example:

>>> converter = QRAC21Converter(instance)
>>> hamiltonian = converter.get_cost_hamiltonian()

Attributes

Methods

get_cost_hamiltonian
def get_cost_hamiltonian(self) -> qm_o.Hamiltonian

Generate the cost Hamiltonian for the QRAC-encoded problem.

Returns:

qm_o.Hamiltonian — Hamiltonian representing the cost function in QRAC form.

get_encoded_pauli_list
def get_encoded_pauli_list(self) -> list[qm_o.Hamiltonian]

Get the encoded Pauli operators as a list of Hamiltonians.

Returns:

list[qm_o.Hamiltonian] — list[Hamiltonian]: List of Hamiltonians for each variable’s Pauli operator.


QRAC21Encoder [source]

class QRAC21Encoder(GraphColoringQRACEncoder)

(2,1,p)-QRAC Encoder.

Encodes Ising model variables into Pauli operators using 2-coloring. Up to 2 variables per qubit, using Z and X Paulis.

The relaxed Hamiltonian is:

H̃ = Σ_{ij} √k_i·√k_j·J_{ij}·P_{f(i)}·P_{f(j)} + Σ_i √k_i·h_i·P_{f(i)}

where k_i is the number of variables encoded on the qubit containing variable i.

Attributes


QRAC31Converter [source]

class QRAC31Converter(QRACConverterBase[QRAC31Encoder])

QRAC(3,1,p) Converter for Quantum Random Access Optimization.

Converts optimization problems into QRAC-encoded form with reduced qubit count. Up to 3 variables are encoded into a single qubit using Pauli operators.

Example:

>>> converter = QRAC31Converter(instance)
>>> hamiltonian = converter.get_cost_hamiltonian()

Attributes

Methods

get_cost_hamiltonian
def get_cost_hamiltonian(self) -> qm_o.Hamiltonian

Generate the cost Hamiltonian for the QRAC-encoded problem.

Returns:

qm_o.Hamiltonian — Hamiltonian representing the cost function in QRAC form.

get_encoded_pauli_list
def get_encoded_pauli_list(self) -> list[qm_o.Hamiltonian]

Get the encoded Pauli operators as a list of Hamiltonians.

Returns:

list[qm_o.Hamiltonian] — list[Hamiltonian]: List of Hamiltonians for each variable’s Pauli operator.


QRAC31Encoder [source]

class QRAC31Encoder(GraphColoringQRACEncoder)

(3,1,p)-QRAC Encoder.

Encodes Ising model variables into Pauli operators on qubits using graph coloring to ensure that interacting variables are assigned to different qubits.

The relaxed Hamiltonian is:

H̃ = Σ_{ij} √k_i·√k_j·J_{ij}·P_{f(i)}·P_{f(j)} + Σ_i √k_i·h_i·P_{f(i)}

where f(i) = (qubit_index, pauli_type) maps variable i to a Pauli operator, and k_i is the number of variables encoded on the qubit containing variable i.

Attributes


QRAC32Converter [source]

class QRAC32Converter(QRACConverterBase[QRAC32Encoder])

QRAC(3,2,p) Converter for Quantum Random Access Optimization.

Converts optimization problems into QRAC-encoded form. Color groups with k>=2 variables use 2-local prime operators (X’, Y’, Z’) on 2 physical qubits. Groups with k=1 use a regular Pauli on 1 physical qubit.

Example:

>>> converter = QRAC32Converter(instance)
>>> hamiltonian = converter.get_cost_hamiltonian()

Attributes

Methods

get_cost_hamiltonian
def get_cost_hamiltonian(self) -> qm_o.Hamiltonian

Generate the cost Hamiltonian for the QRAC-encoded problem.

Returns:

qm_o.Hamiltonian — Hamiltonian representing the cost function in QRAC form.

get_encoded_pauli_list
def get_encoded_pauli_list(self) -> list[qm_o.Hamiltonian]

Get the encoded Pauli operators as a list of Hamiltonians.

Returns:

list[qm_o.Hamiltonian] — list[Hamiltonian]: List of Hamiltonians for each variable’s Pauli operator.


QRAC32Encoder [source]

class QRAC32Encoder(GraphColoringQRACEncoder)

(3,2,p)-QRAC Encoder.

Same graph coloring as (3,1,p) but uses 2-local prime operators for color groups with k>=2 variables. Groups with k=1 use a single physical qubit with a regular Pauli operator.

Physical qubit allocation per color group:

k=1: 1 physical qubit (regular Pauli, scale=1) k=2: 2 physical qubits (prime operators, scale=√(2·2)=2) k=3: 2 physical qubits (prime operators, scale=√(2·3)=√6)

Attributes


QRACSpaceEfficientConverter [source]

class QRACSpaceEfficientConverter(QRACConverterBase[QRACSpaceEfficientEncoder])

Space Efficient QRAC Converter for Quantum Random Access Optimization.

Uses sequential numbering with constant 2:1 compression ratio. No graph coloring is performed.

Example:

>>> converter = QRACSpaceEfficientConverter(instance)
>>> hamiltonian = converter.get_cost_hamiltonian()

Attributes

Methods

get_cost_hamiltonian
def get_cost_hamiltonian(self) -> qm_o.Hamiltonian

Generate the cost Hamiltonian for the space-efficient QRAC-encoded problem.

Returns:

qm_o.Hamiltonian — Hamiltonian representing the cost function in QRAC form.

get_encoded_pauli_list
def get_encoded_pauli_list(self) -> list[qm_o.Hamiltonian]

Get the encoded Pauli operators as a list of Hamiltonians.

Returns:

list[qm_o.Hamiltonian] — list[Hamiltonian]: List of Hamiltonians for each variable’s Pauli operator.


QRACSpaceEfficientEncoder [source]

class QRACSpaceEfficientEncoder(BaseQRACEncoder)

Space Efficient QRAC Encoder.

No graph coloring. Uses sequential numbering: variable i -> qubit i//2, Pauli X (even index) or Y (odd index).

Always maintains a 2:1 compression ratio.

Attributes


SignRounder [source]

class SignRounder

Simple sign-based rounding.

Rounds Pauli expectation values to spin values using the sign function:

Example:

>>> rounder = SignRounder()
>>> expectations = [0.8, -0.3, 0.1]  # Pauli expectation values
>>> spins = rounder.round(expectations)
>>> print(spins)  # [1, -1, 1]

Methods

round
def round(self, expectations: list[float]) -> list[int]

Round Pauli expectations to spin values.

Parameters:

NameTypeDescription
expectationslist[float]List of Pauli expectation values for each variable. Each value should be in range [-1, 1].

Returns:

list[int] — List of spin values (+1 or -1) for each variable


qamomile.optimization.qrao.base_converter

Base class for QRAC-based converters.

This module provides the QRACConverterBase class that implements shared functionality across all QRAC converter variants.

Overview

ClassDescription
MathematicalProblemConverter
QRACConverterBaseAbstract base for all QRAC-based converters.

Classes

MathematicalProblemConverter [source]

class MathematicalProblemConverter(abc.ABC)
Constructor
def __init__(self, instance: ommx.v1.Instance | BinaryModel) -> None
Attributes
Methods
decode
def decode(self, samples: SampleResult[list[int]]) -> BinarySampleSet

Decode quantum measurement results.

Returns results in the original vartype (BINARY or SPIN) that was provided when constructing the converter.

get_cost_hamiltonian
def get_cost_hamiltonian(self) -> qm_o.Hamiltonian

Construct the cost Hamiltonian.

Subclasses must implement this method to build the appropriate Hamiltonian for their specific algorithm (e.g., Pauli-Z for QAOA, QRAC-encoded for QRAO).

Returns:

qm_o.Hamiltonian — qm_o.Hamiltonian: The cost Hamiltonian.


QRACConverterBase [source]

class QRACConverterBase(MathematicalProblemConverter, abc.ABC, Generic[EncoderT])

Abstract base for all QRAC-based converters.

Subclasses must implement:

Ansatz Construction:

This converter does not provide a built-in ansatz, giving users full control over their variational circuit design. Use building blocks from :mod:qamomile.circuit.algorithm.basic to compose your own ansatz:

import qamomile.circuit as qmc
from qamomile.circuit.algorithm.basic import (
    ry_layer, rz_layer, cz_entangling_layer,
)

@qmc.qkernel
def my_ansatz(
    n: qmc.UInt,
    depth: qmc.UInt,
    thetas: qmc.Vector[qmc.Float],
) -> qmc.Vector[qmc.Qubit]:
    q = qmc.allocate(n)
    for i in qmc.range(n):
        q[i] = qmc.h(q[i])
    for d in qmc.range(depth):
        offset = d * 2 * n
        q = ry_layer(q, thetas, offset)
        q = rz_layer(q, thetas, offset + n)
        q = cz_entangling_layer(q)
    return q

Available layers in qamomile.circuit.algorithm.basic:

The total number of variational parameters depends on the ansatz design. For the example above it is 2 * num_qubits * depth.

Attributes
Methods
get_encoded_pauli_list
def get_encoded_pauli_list(self) -> list[qm_o.Hamiltonian]

qamomile.optimization.qrao.base_encoder

QRAC Encoder base classes and utilities.

This module provides:

Concrete encoder classes live alongside their converters:

Overview

FunctionDescription
check_linear_termSearch for items within the index of linear term that have not been assigned to the color_group, and add them.
color_group_to_qrac_encodeEncode a color group mapping into Pauli operator assignments.
greedy_graph_coloringgraph coloring for QRAC
is_close_zeroCheck if a given floating-point value is close to zero within a small tolerance.
ClassDescription
BaseQRACEncoderAbstract base for all QRAC encoders.
GraphColoringQRACEncoderBase for graph-coloring-based QRAC encoders (21, 31, 32).

Functions

check_linear_term [source]

def check_linear_term(
    color_group: dict[int, list[int]],
    linear_term_index: Iterable[int],
    max_color_group_size: int,
) -> dict[int, list[int]]

Search for items within the index of linear term that have not been assigned to the color_group, and add them.

Parameters:

NameTypeDescription
color_groupdict[int, list[int]]color_group
linear_term_indexlist[int]list of index of linear term
max_color_group_sizeintthe maximum number of encoding qubits. if you want to use for the qrac31, set 3.

Returns:

dict[int, list[int]] — dict[int, list[int]]: color_group which added items within the index of linear term that have not been assigned to the color_group.


color_group_to_qrac_encode [source]

def color_group_to_qrac_encode(color_group: dict[int, list[int]]) -> dict[int, qm_o.PauliOperator]

Encode a color group mapping into Pauli operator assignments.

Assigns Z, X, Y Pauli operators (in order) to variables within each qubit’s color group.

Parameters:

NameTypeDescription
color_groupdict[int, list[int]]Mapping from qubit index to list of variable indices.

Returns:

dict[int, qm_o.PauliOperator] — Mapping from variable index to PauliOperator.

Examples:

>>> color_group = {0: [0, 1, 2], 1: [3, 4], 2: [6,]}
>>> color_group_to_qrac_encode(color_group)
{0: Z0, 1: X0, 2: Y0, 3: Z1, 4: X1, 6: Z2}

greedy_graph_coloring [source]

def greedy_graph_coloring(
    graph: Iterable[tuple[int, int]],
    max_color_group_size: int,
    init_coloring: Optional[dict[int, int]] = None,
) -> tuple[dict[int, int], dict[int, list[int]]]

graph coloring for QRAC

Parameters:

NameTypeDescription
graphtyp.Iterable[tuple[int, int]]description
max_color_group_sizeintif you want to use for the qrac31, set 3.
init_coloringtyp.Optional[dict[int, int]]initial coloring. Defaults to None.

Returns:

tuple[dict[int, int], dict[int, list[int]]] — tuple[dict[int, int], dict[int, list[int]]]: coloring, color_group

Examples:

>>> graph = [(0, 1), (1, 2), (2, 0), (0, 4)]
>>> greedy_graph_coloring(graph, 2)
({0: 0, 1: 1, 2: 2, 4: 1}, {0: [0], 1: [1, 4], 2: [2]})

is_close_zero [source]

def is_close_zero(value: float, abs_tol = 1e-15) -> bool

Check if a given floating-point value is close to zero within a small tolerance.

Parameters:

NameTypeDescription
valuefloatThe floating-point value to check.

Returns:

bool — True if the value is close to zero, False otherwise.

Classes

BaseQRACEncoder [source]

class BaseQRACEncoder(abc.ABC)

Abstract base for all QRAC encoders.

Provides:

Subclasses must implement _perform_encoding and num_qubits.

Constructor
def __init__(self, spin_model: BinaryModel) -> None
Attributes
Methods
get_pauli_for_variable
def get_pauli_for_variable(self, var_idx: int) -> tuple[int, PauliType]

Get Pauli operator for a variable.

Parameters:

NameTypeDescription
var_idxintVariable index

Returns:

tuple[int, PauliType] — Tuple of (qubit_index, pauli_type)


GraphColoringQRACEncoder [source]

class GraphColoringQRACEncoder(BaseQRACEncoder, abc.ABC)

Base for graph-coloring-based QRAC encoders (21, 31, 32).

Subclasses only need to set max_color_group_size and paulis.

The encoding process:

  1. Graph coloring on the interaction graph.

  2. Adds linear-only variables to color groups.

  3. Assigns Pauli operators to variables within each qubit.

Constructor
def __init__(self, spin_model: BinaryModel) -> None
Attributes
Methods
encode_ising
def encode_ising(
    self,
    ising: BinaryModel,
) -> tuple[qm_o.Hamiltonian, dict[int, qm_o.PauliOperator]]

Encode a spin model into a relaxed QRAC Hamiltonian.

Uses the encoding determined by _perform_encoding and delegates operator/scale computation to _get_operator_and_scale.

Parameters:

NameTypeDescription
isingBinaryModelBinaryModel in SPIN vartype.

Returns:

tuple[qm_o.Hamiltonian, dict[int, qm_o.PauliOperator]] — Tuple of (relaxed Hamiltonian, encoding map).


qamomile.optimization.qrao.graph_coloring

Overview

FunctionDescription
check_linear_termSearch for items within the index of linear term that have not been assigned to the color_group, and add them.
greedy_graph_coloringgraph coloring for QRAC

Functions

check_linear_term [source]

def check_linear_term(
    color_group: dict[int, list[int]],
    linear_term_index: Iterable[int],
    max_color_group_size: int,
) -> dict[int, list[int]]

Search for items within the index of linear term that have not been assigned to the color_group, and add them.

Parameters:

NameTypeDescription
color_groupdict[int, list[int]]color_group
linear_term_indexlist[int]list of index of linear term
max_color_group_sizeintthe maximum number of encoding qubits. if you want to use for the qrac31, set 3.

Returns:

dict[int, list[int]] — dict[int, list[int]]: color_group which added items within the index of linear term that have not been assigned to the color_group.


greedy_graph_coloring [source]

def greedy_graph_coloring(
    graph: Iterable[tuple[int, int]],
    max_color_group_size: int,
    init_coloring: Optional[dict[int, int]] = None,
) -> tuple[dict[int, int], dict[int, list[int]]]

graph coloring for QRAC

Parameters:

NameTypeDescription
graphtyp.Iterable[tuple[int, int]]description
max_color_group_sizeintif you want to use for the qrac31, set 3.
init_coloringtyp.Optional[dict[int, int]]initial coloring. Defaults to None.

Returns:

tuple[dict[int, int], dict[int, list[int]]] — tuple[dict[int, int], dict[int, list[int]]]: coloring, color_group

Examples:

>>> graph = [(0, 1), (1, 2), (2, 0), (0, 4)]
>>> greedy_graph_coloring(graph, 2)
({0: 0, 1: 1, 2: 2, 4: 1}, {0: [0], 1: [1, 4], 2: [2]})

qamomile.optimization.qrao.qrao21

QRAC(2,1,p) Converter for Quantum Random Access Optimization.

This module provides the QRAC21Converter class that converts optimization problems into QRAC-encoded quantum circuits using (2,1,p)-QRAC. Up to 2 variables are encoded into a single qubit using X and Z Pauli operators.

Overview

ClassDescription
GraphColoringQRACEncoderBase for graph-coloring-based QRAC encoders (21, 31, 32).
QRAC21ConverterQRAC(2,1,p) Converter for Quantum Random Access Optimization.
QRAC21Encoder(2,1,p)-QRAC Encoder.
QRACConverterBaseAbstract base for all QRAC-based converters.

Classes

GraphColoringQRACEncoder [source]

class GraphColoringQRACEncoder(BaseQRACEncoder, abc.ABC)

Base for graph-coloring-based QRAC encoders (21, 31, 32).

Subclasses only need to set max_color_group_size and paulis.

The encoding process:

  1. Graph coloring on the interaction graph.

  2. Adds linear-only variables to color groups.

  3. Assigns Pauli operators to variables within each qubit.

Constructor
def __init__(self, spin_model: BinaryModel) -> None
Attributes
Methods
encode_ising
def encode_ising(
    self,
    ising: BinaryModel,
) -> tuple[qm_o.Hamiltonian, dict[int, qm_o.PauliOperator]]

Encode a spin model into a relaxed QRAC Hamiltonian.

Uses the encoding determined by _perform_encoding and delegates operator/scale computation to _get_operator_and_scale.

Parameters:

NameTypeDescription
isingBinaryModelBinaryModel in SPIN vartype.

Returns:

tuple[qm_o.Hamiltonian, dict[int, qm_o.PauliOperator]] — Tuple of (relaxed Hamiltonian, encoding map).


QRAC21Converter [source]

class QRAC21Converter(QRACConverterBase[QRAC21Encoder])

QRAC(2,1,p) Converter for Quantum Random Access Optimization.

Converts optimization problems into QRAC-encoded form with reduced qubit count. Up to 2 variables are encoded into a single qubit using X and Z Pauli operators.

Example:

>>> converter = QRAC21Converter(instance)
>>> hamiltonian = converter.get_cost_hamiltonian()
Attributes
Methods
get_cost_hamiltonian
def get_cost_hamiltonian(self) -> qm_o.Hamiltonian

Generate the cost Hamiltonian for the QRAC-encoded problem.

Returns:

qm_o.Hamiltonian — Hamiltonian representing the cost function in QRAC form.

get_encoded_pauli_list
def get_encoded_pauli_list(self) -> list[qm_o.Hamiltonian]

Get the encoded Pauli operators as a list of Hamiltonians.

Returns:

list[qm_o.Hamiltonian] — list[Hamiltonian]: List of Hamiltonians for each variable’s Pauli operator.


QRAC21Encoder [source]

class QRAC21Encoder(GraphColoringQRACEncoder)

(2,1,p)-QRAC Encoder.

Encodes Ising model variables into Pauli operators using 2-coloring. Up to 2 variables per qubit, using Z and X Paulis.

The relaxed Hamiltonian is:

H̃ = Σ_{ij} √k_i·√k_j·J_{ij}·P_{f(i)}·P_{f(j)} + Σ_i √k_i·h_i·P_{f(i)}

where k_i is the number of variables encoded on the qubit containing variable i.

Attributes

QRACConverterBase [source]

class QRACConverterBase(MathematicalProblemConverter, abc.ABC, Generic[EncoderT])

Abstract base for all QRAC-based converters.

Subclasses must implement:

Ansatz Construction:

This converter does not provide a built-in ansatz, giving users full control over their variational circuit design. Use building blocks from :mod:qamomile.circuit.algorithm.basic to compose your own ansatz:

import qamomile.circuit as qmc
from qamomile.circuit.algorithm.basic import (
    ry_layer, rz_layer, cz_entangling_layer,
)

@qmc.qkernel
def my_ansatz(
    n: qmc.UInt,
    depth: qmc.UInt,
    thetas: qmc.Vector[qmc.Float],
) -> qmc.Vector[qmc.Qubit]:
    q = qmc.allocate(n)
    for i in qmc.range(n):
        q[i] = qmc.h(q[i])
    for d in qmc.range(depth):
        offset = d * 2 * n
        q = ry_layer(q, thetas, offset)
        q = rz_layer(q, thetas, offset + n)
        q = cz_entangling_layer(q)
    return q

Available layers in qamomile.circuit.algorithm.basic:

The total number of variational parameters depends on the ansatz design. For the example above it is 2 * num_qubits * depth.

Attributes
Methods
get_encoded_pauli_list
def get_encoded_pauli_list(self) -> list[qm_o.Hamiltonian]

qamomile.optimization.qrao.qrao31

QRAC(3,1,p) Converter for Quantum Random Access Optimization.

This module provides the QRAC31Converter class that converts optimization problems into QRAC-encoded quantum circuits and handles result decoding.

Overview

ClassDescription
GraphColoringQRACEncoderBase for graph-coloring-based QRAC encoders (21, 31, 32).
QRAC31ConverterQRAC(3,1,p) Converter for Quantum Random Access Optimization.
QRAC31Encoder(3,1,p)-QRAC Encoder.
QRACConverterBaseAbstract base for all QRAC-based converters.

Classes

GraphColoringQRACEncoder [source]

class GraphColoringQRACEncoder(BaseQRACEncoder, abc.ABC)

Base for graph-coloring-based QRAC encoders (21, 31, 32).

Subclasses only need to set max_color_group_size and paulis.

The encoding process:

  1. Graph coloring on the interaction graph.

  2. Adds linear-only variables to color groups.

  3. Assigns Pauli operators to variables within each qubit.

Constructor
def __init__(self, spin_model: BinaryModel) -> None
Attributes
Methods
encode_ising
def encode_ising(
    self,
    ising: BinaryModel,
) -> tuple[qm_o.Hamiltonian, dict[int, qm_o.PauliOperator]]

Encode a spin model into a relaxed QRAC Hamiltonian.

Uses the encoding determined by _perform_encoding and delegates operator/scale computation to _get_operator_and_scale.

Parameters:

NameTypeDescription
isingBinaryModelBinaryModel in SPIN vartype.

Returns:

tuple[qm_o.Hamiltonian, dict[int, qm_o.PauliOperator]] — Tuple of (relaxed Hamiltonian, encoding map).


QRAC31Converter [source]

class QRAC31Converter(QRACConverterBase[QRAC31Encoder])

QRAC(3,1,p) Converter for Quantum Random Access Optimization.

Converts optimization problems into QRAC-encoded form with reduced qubit count. Up to 3 variables are encoded into a single qubit using Pauli operators.

Example:

>>> converter = QRAC31Converter(instance)
>>> hamiltonian = converter.get_cost_hamiltonian()
Attributes
Methods
get_cost_hamiltonian
def get_cost_hamiltonian(self) -> qm_o.Hamiltonian

Generate the cost Hamiltonian for the QRAC-encoded problem.

Returns:

qm_o.Hamiltonian — Hamiltonian representing the cost function in QRAC form.

get_encoded_pauli_list
def get_encoded_pauli_list(self) -> list[qm_o.Hamiltonian]

Get the encoded Pauli operators as a list of Hamiltonians.

Returns:

list[qm_o.Hamiltonian] — list[Hamiltonian]: List of Hamiltonians for each variable’s Pauli operator.


QRAC31Encoder [source]

class QRAC31Encoder(GraphColoringQRACEncoder)

(3,1,p)-QRAC Encoder.

Encodes Ising model variables into Pauli operators on qubits using graph coloring to ensure that interacting variables are assigned to different qubits.

The relaxed Hamiltonian is:

H̃ = Σ_{ij} √k_i·√k_j·J_{ij}·P_{f(i)}·P_{f(j)} + Σ_i √k_i·h_i·P_{f(i)}

where f(i) = (qubit_index, pauli_type) maps variable i to a Pauli operator, and k_i is the number of variables encoded on the qubit containing variable i.

Attributes

QRACConverterBase [source]

class QRACConverterBase(MathematicalProblemConverter, abc.ABC, Generic[EncoderT])

Abstract base for all QRAC-based converters.

Subclasses must implement:

Ansatz Construction:

This converter does not provide a built-in ansatz, giving users full control over their variational circuit design. Use building blocks from :mod:qamomile.circuit.algorithm.basic to compose your own ansatz:

import qamomile.circuit as qmc
from qamomile.circuit.algorithm.basic import (
    ry_layer, rz_layer, cz_entangling_layer,
)

@qmc.qkernel
def my_ansatz(
    n: qmc.UInt,
    depth: qmc.UInt,
    thetas: qmc.Vector[qmc.Float],
) -> qmc.Vector[qmc.Qubit]:
    q = qmc.allocate(n)
    for i in qmc.range(n):
        q[i] = qmc.h(q[i])
    for d in qmc.range(depth):
        offset = d * 2 * n
        q = ry_layer(q, thetas, offset)
        q = rz_layer(q, thetas, offset + n)
        q = cz_entangling_layer(q)
    return q

Available layers in qamomile.circuit.algorithm.basic:

The total number of variational parameters depends on the ansatz design. For the example above it is 2 * num_qubits * depth.

Attributes
Methods
get_encoded_pauli_list
def get_encoded_pauli_list(self) -> list[qm_o.Hamiltonian]

qamomile.optimization.qrao.qrao32

QRAC(3,2,p) Converter for Quantum Random Access Optimization.

This module provides the QRAC32Converter class that converts optimization problems into QRAC-encoded quantum circuits using (3,2,p)-QRAC.

Color groups with k>=2 variables use 2-local prime operators (X’, Y’, Z’) on 2 physical qubits. Groups with k=1 variable fall back to a regular 1-local Pauli on a single physical qubit.

Overview

FunctionDescription
build_physical_qubit_mapMap color (logical) index to physical qubit start index.
create_prime_operatorConvert a logical PauliOperator to its prime (2-qubit) form.
create_x_primeCreate X’ operator for the given 2-qubit starting index.
create_y_primeCreate Y’ operator for the given 2-qubit starting index.
create_z_primeCreate Z’ operator for the given 2-qubit starting index.
ClassDescription
GraphColoringQRACEncoderBase for graph-coloring-based QRAC encoders (21, 31, 32).
QRAC32ConverterQRAC(3,2,p) Converter for Quantum Random Access Optimization.
QRAC32Encoder(3,2,p)-QRAC Encoder.
QRACConverterBaseAbstract base for all QRAC-based converters.

Functions

build_physical_qubit_map [source]

def build_physical_qubit_map(color_group: dict[int, list[int]]) -> tuple[dict[int, int], int]

Map color (logical) index to physical qubit start index.

For (3,2,p)-QRAC, color groups with k=1 variable use 1 physical qubit (regular Pauli), while groups with k>=2 variables use 2 physical qubits (prime operators).

Parameters:

NameTypeDescription
color_groupdict[int, list[int]]Mapping from color index to list of variable indices.

Returns:

tuple[dict[int, int], int] — Tuple of (color_to_phys_start mapping, total_physical_qubits).


create_prime_operator [source]

def create_prime_operator(pauli_op: qm_o.PauliOperator, phys_start: int) -> qm_o.Hamiltonian

Convert a logical PauliOperator to its prime (2-qubit) form.

Parameters:

NameTypeDescription
pauli_opqm_o.PauliOperatorPauliOperator with logical qubit index.
phys_startintPhysical qubit start index for the prime operator.

Returns:

qm_o.Hamiltonian — Hamiltonian representing the 2-local prime operator.


create_x_prime [source]

def create_x_prime(idx: int) -> qm_o.Hamiltonian

Create X’ operator for the given 2-qubit starting index.

X’ = (1/√6) * (1/2 * X₁X₂ + 1/2 * X₁Z₂ + Z₁I₂)

Parameters:

NameTypeDescription
idxintIndex of the first qubit.

Returns:

qm_o.Hamiltonian — Hamiltonian representing the X’ operator.


create_y_prime [source]

def create_y_prime(idx: int) -> qm_o.Hamiltonian

Create Y’ operator for the given 2-qubit starting index.

Y’ = (1/√6) * (1/2 * I₁X₂ + I₁Z₂ + 1/2 * Y₁Y₂)

Parameters:

NameTypeDescription
idxintIndex of the first qubit.

Returns:

qm_o.Hamiltonian — Hamiltonian representing the Y’ operator.


create_z_prime [source]

def create_z_prime(idx: int) -> qm_o.Hamiltonian

Create Z’ operator for the given 2-qubit starting index.

Z’ = (1/√6) * (Z₁Z₂ - 1/2 * X₁I₂ - 1/2 * Z₁X₂)

Parameters:

NameTypeDescription
idxintIndex of the first qubit.

Returns:

qm_o.Hamiltonian — Hamiltonian representing the Z’ operator.

Classes

GraphColoringQRACEncoder [source]

class GraphColoringQRACEncoder(BaseQRACEncoder, abc.ABC)

Base for graph-coloring-based QRAC encoders (21, 31, 32).

Subclasses only need to set max_color_group_size and paulis.

The encoding process:

  1. Graph coloring on the interaction graph.

  2. Adds linear-only variables to color groups.

  3. Assigns Pauli operators to variables within each qubit.

Constructor
def __init__(self, spin_model: BinaryModel) -> None
Attributes
Methods
encode_ising
def encode_ising(
    self,
    ising: BinaryModel,
) -> tuple[qm_o.Hamiltonian, dict[int, qm_o.PauliOperator]]

Encode a spin model into a relaxed QRAC Hamiltonian.

Uses the encoding determined by _perform_encoding and delegates operator/scale computation to _get_operator_and_scale.

Parameters:

NameTypeDescription
isingBinaryModelBinaryModel in SPIN vartype.

Returns:

tuple[qm_o.Hamiltonian, dict[int, qm_o.PauliOperator]] — Tuple of (relaxed Hamiltonian, encoding map).


QRAC32Converter [source]

class QRAC32Converter(QRACConverterBase[QRAC32Encoder])

QRAC(3,2,p) Converter for Quantum Random Access Optimization.

Converts optimization problems into QRAC-encoded form. Color groups with k>=2 variables use 2-local prime operators (X’, Y’, Z’) on 2 physical qubits. Groups with k=1 use a regular Pauli on 1 physical qubit.

Example:

>>> converter = QRAC32Converter(instance)
>>> hamiltonian = converter.get_cost_hamiltonian()
Attributes
Methods
get_cost_hamiltonian
def get_cost_hamiltonian(self) -> qm_o.Hamiltonian

Generate the cost Hamiltonian for the QRAC-encoded problem.

Returns:

qm_o.Hamiltonian — Hamiltonian representing the cost function in QRAC form.

get_encoded_pauli_list
def get_encoded_pauli_list(self) -> list[qm_o.Hamiltonian]

Get the encoded Pauli operators as a list of Hamiltonians.

Returns:

list[qm_o.Hamiltonian] — list[Hamiltonian]: List of Hamiltonians for each variable’s Pauli operator.


QRAC32Encoder [source]

class QRAC32Encoder(GraphColoringQRACEncoder)

(3,2,p)-QRAC Encoder.

Same graph coloring as (3,1,p) but uses 2-local prime operators for color groups with k>=2 variables. Groups with k=1 use a single physical qubit with a regular Pauli operator.

Physical qubit allocation per color group:

k=1: 1 physical qubit (regular Pauli, scale=1) k=2: 2 physical qubits (prime operators, scale=√(2·2)=2) k=3: 2 physical qubits (prime operators, scale=√(2·3)=√6)

Attributes

QRACConverterBase [source]

class QRACConverterBase(MathematicalProblemConverter, abc.ABC, Generic[EncoderT])

Abstract base for all QRAC-based converters.

Subclasses must implement:

Ansatz Construction:

This converter does not provide a built-in ansatz, giving users full control over their variational circuit design. Use building blocks from :mod:qamomile.circuit.algorithm.basic to compose your own ansatz:

import qamomile.circuit as qmc
from qamomile.circuit.algorithm.basic import (
    ry_layer, rz_layer, cz_entangling_layer,
)

@qmc.qkernel
def my_ansatz(
    n: qmc.UInt,
    depth: qmc.UInt,
    thetas: qmc.Vector[qmc.Float],
) -> qmc.Vector[qmc.Qubit]:
    q = qmc.allocate(n)
    for i in qmc.range(n):
        q[i] = qmc.h(q[i])
    for d in qmc.range(depth):
        offset = d * 2 * n
        q = ry_layer(q, thetas, offset)
        q = rz_layer(q, thetas, offset + n)
        q = cz_entangling_layer(q)
    return q

Available layers in qamomile.circuit.algorithm.basic:

The total number of variational parameters depends on the ansatz design. For the example above it is 2 * num_qubits * depth.

Attributes
Methods
get_encoded_pauli_list
def get_encoded_pauli_list(self) -> list[qm_o.Hamiltonian]

qamomile.optimization.qrao.qrao_space_efficient

Space Efficient QRAC Converter for Quantum Random Access Optimization.

This module implements Space Efficient QRAO which maintains a constant 2:1 compression ratio by using sequential numbering instead of graph coloring.

Variables are alternately assigned X and Y Pauli operators on consecutive qubits. When interacting variables share the same qubit, a Z operator is used instead of the product of their Paulis.

Overview

FunctionDescription
is_close_zeroCheck if a given floating-point value is close to zero within a small tolerance.
numbering_space_efficient_encodeEncode using sequential numbering.
qrac_space_efficient_encode_isingEncode a spin model using space-efficient QRAC.
ClassDescription
BaseQRACEncoderAbstract base for all QRAC encoders.
QRACConverterBaseAbstract base for all QRAC-based converters.
QRACSpaceEfficientConverterSpace Efficient QRAC Converter for Quantum Random Access Optimization.
QRACSpaceEfficientEncoderSpace Efficient QRAC Encoder.

Functions

is_close_zero [source]

def is_close_zero(value: float, abs_tol = 1e-15) -> bool

Check if a given floating-point value is close to zero within a small tolerance.

Parameters:

NameTypeDescription
valuefloatThe floating-point value to check.

Returns:

bool — True if the value is close to zero, False otherwise.


numbering_space_efficient_encode [source]

def numbering_space_efficient_encode(ising: BinaryModel[Literal[VarType.SPIN]]) -> dict[int, qm_o.PauliOperator]

Encode using sequential numbering.

Variable i is assigned to qubit i//2 with Pauli X (even) or Y (odd).

Parameters:

NameTypeDescription
isingBinaryModel[typing.Literal[VarType.SPIN]]BinaryModel in SPIN vartype.

Returns:

dict[int, qm_o.PauliOperator] — Mapping from variable index to PauliOperator.


qrac_space_efficient_encode_ising [source]

def qrac_space_efficient_encode_ising(
    ising: BinaryModel[Literal[VarType.SPIN]],
) -> tuple[qm_o.Hamiltonian, dict[int, qm_o.PauliOperator]]

Encode a spin model using space-efficient QRAC.

For quadratic terms:

Parameters:

NameTypeDescription
isingBinaryModel[typing.Literal[VarType.SPIN]]BinaryModel in SPIN vartype.

Returns:

tuple[qm_o.Hamiltonian, dict[int, qm_o.PauliOperator]] — Tuple of (relaxed Hamiltonian, encoding map).

Classes

BaseQRACEncoder [source]

class BaseQRACEncoder(abc.ABC)

Abstract base for all QRAC encoders.

Provides:

Subclasses must implement _perform_encoding and num_qubits.

Constructor
def __init__(self, spin_model: BinaryModel) -> None
Attributes
Methods
get_pauli_for_variable
def get_pauli_for_variable(self, var_idx: int) -> tuple[int, PauliType]

Get Pauli operator for a variable.

Parameters:

NameTypeDescription
var_idxintVariable index

Returns:

tuple[int, PauliType] — Tuple of (qubit_index, pauli_type)


QRACConverterBase [source]

class QRACConverterBase(MathematicalProblemConverter, abc.ABC, Generic[EncoderT])

Abstract base for all QRAC-based converters.

Subclasses must implement:

Ansatz Construction:

This converter does not provide a built-in ansatz, giving users full control over their variational circuit design. Use building blocks from :mod:qamomile.circuit.algorithm.basic to compose your own ansatz:

import qamomile.circuit as qmc
from qamomile.circuit.algorithm.basic import (
    ry_layer, rz_layer, cz_entangling_layer,
)

@qmc.qkernel
def my_ansatz(
    n: qmc.UInt,
    depth: qmc.UInt,
    thetas: qmc.Vector[qmc.Float],
) -> qmc.Vector[qmc.Qubit]:
    q = qmc.allocate(n)
    for i in qmc.range(n):
        q[i] = qmc.h(q[i])
    for d in qmc.range(depth):
        offset = d * 2 * n
        q = ry_layer(q, thetas, offset)
        q = rz_layer(q, thetas, offset + n)
        q = cz_entangling_layer(q)
    return q

Available layers in qamomile.circuit.algorithm.basic:

The total number of variational parameters depends on the ansatz design. For the example above it is 2 * num_qubits * depth.

Attributes
Methods
get_encoded_pauli_list
def get_encoded_pauli_list(self) -> list[qm_o.Hamiltonian]

QRACSpaceEfficientConverter [source]

class QRACSpaceEfficientConverter(QRACConverterBase[QRACSpaceEfficientEncoder])

Space Efficient QRAC Converter for Quantum Random Access Optimization.

Uses sequential numbering with constant 2:1 compression ratio. No graph coloring is performed.

Example:

>>> converter = QRACSpaceEfficientConverter(instance)
>>> hamiltonian = converter.get_cost_hamiltonian()
Attributes
Methods
get_cost_hamiltonian
def get_cost_hamiltonian(self) -> qm_o.Hamiltonian

Generate the cost Hamiltonian for the space-efficient QRAC-encoded problem.

Returns:

qm_o.Hamiltonian — Hamiltonian representing the cost function in QRAC form.

get_encoded_pauli_list
def get_encoded_pauli_list(self) -> list[qm_o.Hamiltonian]

Get the encoded Pauli operators as a list of Hamiltonians.

Returns:

list[qm_o.Hamiltonian] — list[Hamiltonian]: List of Hamiltonians for each variable’s Pauli operator.


QRACSpaceEfficientEncoder [source]

class QRACSpaceEfficientEncoder(BaseQRACEncoder)

Space Efficient QRAC Encoder.

No graph coloring. Uses sequential numbering: variable i -> qubit i//2, Pauli X (even index) or Y (odd index).

Always maintains a 2:1 compression ratio.

Attributes

qamomile.optimization.qrao.rounding

Rounding algorithms for QRAO.

This module provides rounding algorithms that convert Pauli expectation values into discrete spin values (+1 or -1).

Overview

ClassDescription
SignRounderSimple sign-based rounding.

Classes

SignRounder [source]

class SignRounder

Simple sign-based rounding.

Rounds Pauli expectation values to spin values using the sign function:

Example:

>>> rounder = SignRounder()
>>> expectations = [0.8, -0.3, 0.1]  # Pauli expectation values
>>> spins = rounder.round(expectations)
>>> print(spins)  # [1, -1, 1]
Methods
round
def round(self, expectations: list[float]) -> list[int]

Round Pauli expectations to spin values.

Parameters:

NameTypeDescription
expectationslist[float]List of Pauli expectation values for each variable. Each value should be in range [-1, 1].

Returns:

list[int] — List of spin values (+1 or -1) for each variable