Skip to content
This repository has been archived by the owner on Dec 7, 2021. It is now read-only.

Added linear option to IsingToQuadraticProgram converter #956

Merged
merged 17 commits into from
May 11, 2020

Conversation

a-matsuo
Copy link
Contributor

@a-matsuo a-matsuo commented May 7, 2020

Summary

Added linear option to IsingToQuadraticProgram 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.

@a-matsuo a-matsuo changed the title added a linear option to IsingToQuadraticProgram class Added linear option to IsingToQuadraticProgram converter May 7, 2020
@@ -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."""
Copy link
Contributor

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

Copy link
Contributor Author

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:
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. I removed it.

stefan-woerner
stefan-woerner previously approved these changes May 8, 2020
@@ -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
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, I fixed it.

stefan-woerner
stefan-woerner previously approved these changes May 9, 2020
@a-matsuo
Copy link
Contributor Author

It seems that Travis is outputting some error, but I don't know how to fix it. Could anybody please help me?

Warning, treated as error: 21498/home/travis/build/Qiskit/qiskit-aqua/qiskit/optimization/converters/ising_to_quadratic_program.py:docstring of qiskit.optimization.converters.IsingToQuadraticProgram:6:Unexpected indentation. 21499Makefile:28: recipe for target 'html' failed 21500make[1]: *** [html] Error 2 21501make[1]: Leaving directory '/home/travis/build/Qiskit/qiskit-aqua/docs' 21502Makefile:62: recipe for target 'html' failed 21503make: *** [html] Error 2 21504The command "make html SPHINXOPTS=-W" exited with 2.

@manoelmarques
Copy link
Collaborator

manoelmarques commented May 11, 2020

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.

@Cryoris
Copy link
Contributor

Cryoris commented May 11, 2020

Adding to manoel's comment: you could also convert it to a raw string by adding r to and use latex math to render the math correctly.

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 🙂

@a-matsuo
Copy link
Contributor Author

@manoelmarques
Ah, I see. Thank you very much!

@Cryoris
Hmm... I'm not familiar with docstrings, but I want to try the way you mentioned, because it sounds better. How can I add the r option?

Comment on lines 31 to 36
"""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
"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""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
"""

Copy link
Contributor

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

Copy link
Contributor Author

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

@manoelmarques manoelmarques merged commit a5d1c3c into qiskit-community:master May 11, 2020
@a-matsuo
Copy link
Contributor Author

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!

@a-matsuo a-matsuo deleted the ising_to_qp_quadratic branch May 12, 2020 02:15
manoelmarques added a commit to qiskit-community/qiskit-optimization that referenced this pull request Jan 14, 2021
…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>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

IsingToQuadraticProgram converter should only return quadratic part of objective
6 participants