Skip to content

quast_decisiontree.algorithms.classical.tabu

quast_decisiontree.algorithms.classical.tabu

tabu solver from dwave-tabu

TabuSolver

Bases: ClassicalAlgorithm

wrapper for the dwave tabu sampler

Source code in src/quast_decisiontree/algorithms/classical/tabu.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
class TabuSolver(ClassicalAlgorithm):
    """wrapper for the dwave tabu sampler"""

    def __init__(self, num_reads: int = 1, seed=None):
        """initializes the TabuSolver

        Args:
            num_reads (int, optional): How many samples to generate. Defaults to 1.
        """
        self.num_reads = num_reads
        self.sampler = TabuSampler()
        self.seed = seed

    @classmethod
    def build_fake_eigenstate(cls, tabu_result: SampleSet, probabilities: bool = False):
        """generate a fake eigenstate from a Tabu sampling result.

        Args:
            tabu_result (SampleSet): the sampling results
            probabilities (bool, optional): whether to convert the shot counts to probabilities."
            " Defaults to True.
        """
        data = tabu_result.record
        out = dict()
        total_shots = 0
        for line in data:
            key = "".join([str(x) for x in line.sample[::-1]])
            val = line.num_occurrences
            out[key] = val
            total_shots += val

        if probabilities:
            for key, val in out.items():
                out[key] = val / total_shots

        return out

    @classmethod
    def build_fake_eigenvalue(cls, tabu_result: SampleSet):
        """generates a fake eigenvalue from a Tabu sampling result"""
        data = tabu_result.record
        nom = 0
        denom = 0
        for line in data:
            nom += line.num_occurrences * line.energy
            denom += line.num_occurrences
        return nom / denom

    @classmethod
    def check_input(cls, opt_problem: Any) -> bool:
        """Check whether ``opt_problem`` is a valid QUBO input.

        Args:
            opt_problem: The problem description to check.

        Returns:
            True if ``opt_problem`` is a valid QUBO matrix, False otherwise.
        """
        return is_qubo_matrix(opt_problem)

    def execute(self, opt_problem: Any, classical_args: Any = None) -> SampleSet:  # pylint: disable=unused-argument
        """Sample the QUBO with the tabu sampler and return aggregated results.

        Args:
            opt_problem: A QUBO matrix whose bilinear form is minimized.
            classical_args: Present for signature compatibility; unused.

        Returns:
            An aggregated :class:`dimod.SampleSet`.

        Raises:
            TypeError: If ``opt_problem`` is not a valid QUBO matrix.
        """
        if not self.check_input(opt_problem):
            raise TypeError(
                "Invalid input for solver. Either not array-like or not quadratic and 2D"
            )
        result = self.sampler.sample_qubo(opt_problem, num_reads=self.num_reads, seed=self.seed)
        return result.aggregate()

num_reads instance-attribute

num_reads = num_reads

sampler instance-attribute

sampler = TabuSampler()

seed instance-attribute

seed = seed

__init__

__init__(num_reads=1, seed=None)

initializes the TabuSolver

Parameters:

Name Type Description Default
num_reads int

How many samples to generate. Defaults to 1.

1
Source code in src/quast_decisiontree/algorithms/classical/tabu.py
24
25
26
27
28
29
30
31
32
def __init__(self, num_reads: int = 1, seed=None):
    """initializes the TabuSolver

    Args:
        num_reads (int, optional): How many samples to generate. Defaults to 1.
    """
    self.num_reads = num_reads
    self.sampler = TabuSampler()
    self.seed = seed

build_fake_eigenstate classmethod

build_fake_eigenstate(tabu_result, probabilities=False)

generate a fake eigenstate from a Tabu sampling result.

Parameters:

Name Type Description Default
tabu_result SampleSet

the sampling results

required
probabilities bool

whether to convert the shot counts to probabilities."

False
Source code in src/quast_decisiontree/algorithms/classical/tabu.py
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
@classmethod
def build_fake_eigenstate(cls, tabu_result: SampleSet, probabilities: bool = False):
    """generate a fake eigenstate from a Tabu sampling result.

    Args:
        tabu_result (SampleSet): the sampling results
        probabilities (bool, optional): whether to convert the shot counts to probabilities."
        " Defaults to True.
    """
    data = tabu_result.record
    out = dict()
    total_shots = 0
    for line in data:
        key = "".join([str(x) for x in line.sample[::-1]])
        val = line.num_occurrences
        out[key] = val
        total_shots += val

    if probabilities:
        for key, val in out.items():
            out[key] = val / total_shots

    return out

build_fake_eigenvalue classmethod

build_fake_eigenvalue(tabu_result)

generates a fake eigenvalue from a Tabu sampling result

Source code in src/quast_decisiontree/algorithms/classical/tabu.py
58
59
60
61
62
63
64
65
66
67
@classmethod
def build_fake_eigenvalue(cls, tabu_result: SampleSet):
    """generates a fake eigenvalue from a Tabu sampling result"""
    data = tabu_result.record
    nom = 0
    denom = 0
    for line in data:
        nom += line.num_occurrences * line.energy
        denom += line.num_occurrences
    return nom / denom

check_input classmethod

check_input(opt_problem)

Check whether opt_problem is a valid QUBO input.

Parameters:

Name Type Description Default
opt_problem Any

The problem description to check.

required

Returns:

Type Description
bool

True if opt_problem is a valid QUBO matrix, False otherwise.

Source code in src/quast_decisiontree/algorithms/classical/tabu.py
69
70
71
72
73
74
75
76
77
78
79
@classmethod
def check_input(cls, opt_problem: Any) -> bool:
    """Check whether ``opt_problem`` is a valid QUBO input.

    Args:
        opt_problem: The problem description to check.

    Returns:
        True if ``opt_problem`` is a valid QUBO matrix, False otherwise.
    """
    return is_qubo_matrix(opt_problem)

execute

execute(opt_problem, classical_args=None)

Sample the QUBO with the tabu sampler and return aggregated results.

Parameters:

Name Type Description Default
opt_problem Any

A QUBO matrix whose bilinear form is minimized.

required
classical_args Any

Present for signature compatibility; unused.

None

Returns:

Type Description
SampleSet

An aggregated :class:dimod.SampleSet.

Raises:

Type Description
TypeError

If opt_problem is not a valid QUBO matrix.

Source code in src/quast_decisiontree/algorithms/classical/tabu.py
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
def execute(self, opt_problem: Any, classical_args: Any = None) -> SampleSet:  # pylint: disable=unused-argument
    """Sample the QUBO with the tabu sampler and return aggregated results.

    Args:
        opt_problem: A QUBO matrix whose bilinear form is minimized.
        classical_args: Present for signature compatibility; unused.

    Returns:
        An aggregated :class:`dimod.SampleSet`.

    Raises:
        TypeError: If ``opt_problem`` is not a valid QUBO matrix.
    """
    if not self.check_input(opt_problem):
        raise TypeError(
            "Invalid input for solver. Either not array-like or not quadratic and 2D"
        )
    result = self.sampler.sample_qubo(opt_problem, num_reads=self.num_reads, seed=self.seed)
    return result.aggregate()