Coverage for src/braket/annealing/problem.py : 100%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Copyright 2019-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License"). You
4# may not use this file except in compliance with the License. A copy of
5# the License is located at
6#
7# http://aws.amazon.com/apache2.0/
8#
9# or in the "license" file accompanying this file. This file is
10# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11# ANY KIND, either express or implied. See the License for the specific
12# language governing permissions and limitations under the License.
14from __future__ import annotations
16from enum import Enum
17from typing import Dict, Tuple
19import braket.ir.annealing as ir
22class ProblemType(str, Enum):
23 """ The type of annealing problem.
24 QUBO: Quadratic Unconstrained Binary Optimization, with values 1 and 0
25 ISING: Ising model, with values +/-1
26 """
28 QUBO = "QUBO"
29 ISING = "ISING"
32class Problem:
33 """ Represents an annealing problem.
35 """
37 def __init__(
38 self,
39 problem_type: ProblemType,
40 linear: Dict[int, float] = None,
41 quadratic: Dict[Tuple[int, int], float] = None,
42 ):
43 """
45 Args:
46 problem_type (ProblemType): The type of annealing problem
47 linear (Dict[int, float]): The linear terms of this problem,
48 as a map of variable to coefficient
49 quadratic (Dict[Tuple[int, int], float]): The quadratic terms of this problem,
50 as a map of variables to coefficient
52 Examples:
53 >>> problem = Problem(
54 >>> ProblemType.ISING,
55 >>> linear={1: 3.14},
56 >>> quadratic={(1, 2): 10.08},
57 >>> )
58 >>> problem.add_linear_term(2, 1.618).add_quadratic_term((3, 4), 1337)
59 """
60 self._problem_type = problem_type
61 self._linear = linear or {}
62 self._quadratic = quadratic or {}
64 @property
65 def problem_type(self) -> ProblemType:
66 """ The type of annealing problem.
68 Returns:
69 ProblemType: The type of annealing problem
70 """
71 return self._problem_type
73 @property
74 def linear(self) -> Dict[int, float]:
75 """ The linear terms of this problem.
77 Returns:
78 Dict[int, float]: The linear terms of this problem, as a map of variable to coefficient
79 """
80 return self._linear
82 @property
83 def quadratic(self) -> Dict[Tuple[int, int], float]:
84 """ The quadratic terms of this problem.
86 Returns:
87 Dict[Tuple[int, int], float]: The quadratic terms of this problem,
88 as a map of variables to coefficient
89 """
90 return self._quadratic
92 def add_linear_term(self, term: int, coefficient: float) -> Problem:
93 """ Adds a linear term to the problem.
95 Args:
96 term (int): The variable of the linear term
97 coefficient (float): The coefficient of the linear term
99 Returns:
100 Problem: This problem object
101 """
102 self._linear[term] = coefficient
103 return self
105 def add_linear_terms(self, coefficients: Dict[int, float]) -> Problem:
106 """ Adds linear terms to the problem.
108 Args:
109 coefficients (Dict[int, float]): A map of variable to coefficient
111 Returns:
112 Problem: This problem object
113 """
114 self._linear.update(coefficients)
115 return self
117 def add_quadratic_term(self, term: Tuple[int, int], coefficient: float) -> Problem:
118 """ Adds a quadratic term to the problem.
120 Args:
121 term (Tuple[int, int]): The variables of the quadratic term
122 coefficient (flost): The coefficient of the quadratic term
124 Returns:
125 Problem: This problem object
126 """
127 self._quadratic[term] = coefficient
128 return self
130 def add_quadratic_terms(self, coefficients: Dict[Tuple[int, int], float]) -> Problem:
131 """ Adds quadratic terms to the problem.
133 Args:
134 coefficients (Dict[Tuple[int, int], float]): A map of variables to coefficient
136 Returns:
137 Problem: This problem object
138 """
139 self._quadratic.update(coefficients)
140 return self
142 def to_ir(self):
143 """ Converts this problem into IR representation.
145 Returns:
146 ir.Problem: IR representation of this problem object
147 """
148 return ir.Problem(
149 type=ir.ProblemType[self._problem_type.value],
150 linear=self._linear,
151 quadratic={
152 ",".join((str(q1), str(q2))): self._quadratic[(q1, q2)]
153 for q1, q2 in self._quadratic
154 },
155 )