-
Notifications
You must be signed in to change notification settings - Fork 376
Added linear option to IsingToQuadraticProgram converter #956
Added linear option to IsingToQuadraticProgram converter #956
Conversation
@@ -27,13 +27,16 @@ | |||
class IsingToQuadraticProgram: | |||
"""Convert a qubit operator into a quadratic program""" | |||
|
|||
def __init__(self) -> None: | |||
def __init__(self, linear: bool = False) -> None: | |||
"""Initialize the internal data structure.""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need to add docstring for linear
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops, Alright. I added doctoring for linear
# Sub the weight of the linear pauli term from the QUBO matrix | ||
self._qubo_matrix[i, i] -= weight | ||
offset += weight | ||
|
||
self._qp.minimize(offset, linear, quadratic) | ||
if self._linear: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This case distinction is not necessary, as "linear_terms" will just be empty if linear == False.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. I removed it.
…ua into ising_to_qp_quadratic
@@ -77,7 +83,7 @@ def encode(self, qubit_op: WeightedPauliOperator, offset: float = 0.0) -> Quadra | |||
# The coefficient of the quadratic term in `QuadraticProgram` is | |||
# 4 * weight of the pauli | |||
coef = weight * 4 | |||
quadratic[(i, j)] = coef | |||
quadratic_terms[(i, j)] = coef |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to use ()
. quadratic_terms[i, j]
should work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, I fixed it.
…ua into ising_to_qp_quadratic
It seems that Travis is outputting some error, but I don't know how to fix it. Could anybody please help me?
|
File: ising_to_quadratic_program.py init method, you are missing a blank line between the docstring explanation and the 'Args:' , The other methods have the blank line. |
Adding to manoel's comment: you could also convert it to a raw string by adding Also the first line "Initialize..." can be dropped, this looks better in the documentation without these single-liners that just say that this is the initializer 🙂 |
@manoelmarques @Cryoris |
"""Initialize the internal data structure. | ||
Args: | ||
linear: If linear is True, x^2 is treated as a linear term | ||
since x^2 = x for x in {0,1}. | ||
Else, x^2 is treat as a quadratic term | ||
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"""Initialize the internal data structure. | |
Args: | |
linear: If linear is True, x^2 is treated as a linear term | |
since x^2 = x for x in {0,1}. | |
Else, x^2 is treat as a quadratic term | |
""" | |
r""" | |
Args: | |
linear: If True, :math:`x^2` is treated as a linear term | |
since :math:`x^2 = x` for :math:`x \in \{0,1\}`. | |
Else, :math:`x^2` is treat as a quadratic term | |
""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@a-matsuo, see this for the r
option and math
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I got it! Thank you very much for your kind and quick responses :D
add blank line to docstring to fix identation error.
remove spaces
Oh, it's already merged. I was planning to fix the docstring today since it was late last night in Japan. Anyway, I'll make a new PR and fix the docstring. Thank you! |
…unity/qiskit-aqua#956) * added a linear option to IsingToQuadraticProgram class * Added comments * added docstrings * deleted the last case distinction * removed brakets of keys in a dict Co-authored-by: Manoel Marques <manoel@us.ibm.com> Co-authored-by: Julien Gacon <jules.gacon@googlemail.com>
Summary
Added
linear
option toIsingToQuadraticProgram
to fixed #955.Details and comments
If linear is True, x^2 is treated as a linear term since x^2 = x for$x \in {0,1}$ .
Else, x^2 is treat as a quadratic term.
The default setting is False.