Skip to content

Commit

Permalink
change option name from quantum_info to opflow
Browse files Browse the repository at this point in the history
  • Loading branch information
t-imamichi committed Jan 24, 2023
1 parent 995a1d6 commit a914746
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 65 deletions.
4 changes: 2 additions & 2 deletions qiskit_optimization/algorithms/minimum_eigen_optimizer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2020, 2022.
# (C) Copyright IBM 2020, 2023.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
Expand Down Expand Up @@ -222,7 +222,7 @@ def solve(self, problem: QuadraticProgram) -> MinimumEigenOptimizationResult:
problem_ = self._convert(problem, self._converters)

# construct operator and offset
operator, offset = problem_.to_ising()
operator, offset = problem_.to_ising(opflow=True)

return self._solve_internal(operator, offset, problem_, problem)

Expand Down
4 changes: 2 additions & 2 deletions qiskit_optimization/algorithms/warm_start_qaoa_optimizer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2021, 2022.
# (C) Copyright IBM 2021, 2023.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
Expand Down Expand Up @@ -311,7 +311,7 @@ def solve(self, problem: QuadraticProgram) -> MinimumEigenOptimizationResult:
pre_solutions = opt_result.samples[:num_pre_solutions]

# construct operator and offset
operator, offset = converted_problem.to_ising()
operator, offset = converted_problem.to_ising(opflow=True)

results: List[MinimumEigenOptimizationResult] = []
for pre_solution in pre_solutions:
Expand Down
8 changes: 4 additions & 4 deletions qiskit_optimization/problems/quadratic_program.py
Original file line number Diff line number Diff line change
Expand Up @@ -1010,7 +1010,7 @@ def substitute_variables(
return substitute_variables(self, constants, variables)

def to_ising(
self, quantum_info: bool = False
self, opflow: Optional[bool] = None
) -> Tuple[Union[OperatorBase, SparsePauliOp], float]:
"""Return the Ising Hamiltonian of this problem.
Expand All @@ -1019,8 +1019,8 @@ def to_ising(
See https://github.com/Qiskit/qiskit-terra/issues/1148 for details.
Args:
quantum_info: The output object is ``SparsePauliOp`` if True.
Otherwise, it is an OpFlow's operator. (default: False)
opflow: The output object is an OpFlow's operator if True.
Otherwise, it is ``SparsePauliOp``. (default: True)
Returns:
qubit_op: The qubit operator for the problem
Expand All @@ -1033,7 +1033,7 @@ def to_ising(
# pylint: disable=cyclic-import
from ..translators.ising import to_ising

return to_ising(self, quantum_info=quantum_info)
return to_ising(self, opflow=opflow)

def from_ising(
self,
Expand Down
20 changes: 15 additions & 5 deletions qiskit_optimization/translators/ising.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"""Translator between an Ising Hamiltonian and a quadratic program"""

import math
from typing import Tuple, Union
from typing import Optional, Tuple, Union
from warnings import warn

import numpy as np
from qiskit.opflow import I, ListOp, OperatorBase, PauliOp, PauliSumOp, SummedOp
Expand All @@ -25,7 +26,7 @@


def to_ising(
quad_prog: QuadraticProgram, quantum_info: bool = False
quad_prog: QuadraticProgram, opflow: Optional[bool] = None
) -> Tuple[Union[OperatorBase, SparsePauliOp], float]:
"""Return the Ising Hamiltonian of this problem.
Expand All @@ -35,8 +36,8 @@ def to_ising(
Args:
quad_prog: The problem to be translated.
quantum_info: The output object is ``SparsePauliOp`` if True.
Otherwise, it is an OpFlow's operator. (default: False)
opflow: The output object is an OpFlow's operator if True.
Otherwise, it is ``SparsePauliOp``. (default: True)
Returns:
A tuple (qubit_op, offset) comprising the qubit operator for the problem
Expand All @@ -47,6 +48,15 @@ def to_ising(
in the problem.
QiskitOptimizationError: If constraints exist in the problem.
"""
if opflow is None:
opflow = True
warn(
"`opflow` option of `to_ising` is not set explicitly. "
"It is set True now, but the default value will be changed to False. "
"We suggest setting `opflow=True` explicitly like `to_ising(qp, opflow=True)`.",
stacklevel=2,
)

# if problem has variables that are not binary, raise an error
if quad_prog.get_num_vars() > quad_prog.get_num_binary_vars():
raise QiskitOptimizationError(
Expand Down Expand Up @@ -122,7 +132,7 @@ def to_ising(
num_nodes = max(1, num_nodes)
qubit_op = 0 * I ^ num_nodes

if quantum_info:
if not opflow:
# Note: if there is only one operator in `qubit_op`, `qubit_op.primitives`
# can be `Pauli` and can be multiplied only by 1, -1j, -1, 1j.
# So, we need to make sure to generate `SparsePauliOp`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ features:
- |
Updates :func:`~.to_ising` and :func:`~.from_ising` to support
``qiskit.quantum_info.SparsePauliOp``.
Adds ``quantum_info`` option to :func:`~.to_ising` to control the output is
opflow's operator (if ``False``) or ``SparsePauliOp`` (if ``True``).
Adds ``opflow`` option to :func:`~.to_ising` to control the output is
opflow's operator (if ``True``) or ``SparsePauliOp`` (if ``False``).
The default of ``opflow`` option is ``True`` now, but it will be
changed to ``False`` and the option itself will be deprecated
in the future release.
10 changes: 5 additions & 5 deletions test/converters/test_converters.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2020, 2022.
# (C) Copyright IBM 2020, 2023.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
Expand Down Expand Up @@ -62,7 +62,7 @@ def test_empty_problem(self):
op = conv.convert(op)
conv = MaximizeToMinimize()
op = conv.convert(op)
_, shift = op.to_ising()
_, shift = op.to_ising(opflow=True)
self.assertEqual(shift, 0.0)

def test_valid_variable_type(self):
Expand All @@ -71,12 +71,12 @@ def test_valid_variable_type(self):
with self.assertRaises(QiskitOptimizationError):
op = QuadraticProgram()
op.integer_var(0, 10, "int_var")
_ = op.to_ising()
_ = op.to_ising(opflow=True)
# Continuous variable
with self.assertRaises(QiskitOptimizationError):
op = QuadraticProgram()
op.continuous_var(0, 10, "continuous_var")
_ = op.to_ising()
_ = op.to_ising(opflow=True)

def test_inequality_binary(self):
"""Test InequalityToEqualityConverter with binary variables"""
Expand Down Expand Up @@ -397,7 +397,7 @@ def test_optimizationproblem_to_ising(self):
op.linear_constraint(linear, Constraint.Sense.EQ, 3, "sum1")
penalize = LinearEqualityToPenalty(penalty=1e5)
op2 = penalize.convert(op)
qubitop, offset = op2.to_ising()
qubitop, offset = op2.to_ising(opflow=True)
self.assertEqual(qubitop, QUBIT_OP_MAXIMIZE_SAMPLE)
self.assertEqual(offset, OFFSET_MAXIMIZE_SAMPLE)

Expand Down
Loading

0 comments on commit a914746

Please sign in to comment.