# Buggy code
def solve(self, problem: QuadraticProgram) -> MinimumEigenOptimizerResult:
    """Tries to solves the given problem using the optimizer.

    Runs the optimizer to try to solve the optimization problem.

    Args:
        problem: The problem to be solved.

    Returns:
        The result of the optimizer applied to the problem.

    Raises:
        QiskitOptimizationError: If problem not compatible.
    """
    # check compatibility and raise exception if incompatible
    msg = self.get_compatibility_msg(problem)
    if len(msg) > 0:
        raise QiskitOptimizationError("Incompatible problem: {}".format(msg))

    # convert problem to QUBO
    qubo_converter = QuadraticProgramToQubo()
    problem_ = qubo_converter.encode(problem)

    # construct operator and offset
    operator_converter = QuadraticProgramToIsing()
    operator, offset = operator_converter.encode(problem_)

    # approximate ground state of operator using min eigen solver
    eigen_results = self._min_eigen_solver.compute_minimum_eigenvalue(operator)

    # analyze results
    samples = eigenvector_to_solutions(eigen_results.eigenstate, operator)
    samples = [
        (res[0], problem_.objective.sense.value * (res[1] + offset), res[2])
        for res in samples
    ]
    samples.sort(key=lambda x: problem_.objective.sense.value * x[1])

    # translate result back to integers
    opt_res = MinimumEigenOptimizerResult(
        samples[0][0], samples[0][1], samples, qubo_converter
    )
    opt_res = qubo_converter.decode(opt_res)

    # translate results back to original problem
    return opt_res


# Traceback (QiskitOptimizationError)
------------------
rqaoa_result = rqaoa.solve(qubo)
print(rqaoa_result)
------------------

---------------------------------------------------------------------------
QiskitOptimizationError                   Traceback (most recent call last)
<ipython-input-11-6852684b0186> in <module>
----> 1 rqaoa_result = rqaoa.solve(qubo)
2 print(rqaoa_result)

/tmp/docs_build/lib/python3.8/site-packages/qiskit/optimization/algorithms/recursive_minimum_eigen_optimizer.py in solve(self, problem)
171
172                 # 2. replace x_i by -x_j
--> 173                 problem_ = problem_.substitute_variables(variables={i: (j, -1)})
174                 if problem_.status == QuadraticProgram.Status.INFEASIBLE:
175                     raise QiskitOptimizationError('Infeasible due to variable substitution')

/tmp/docs_build/lib/python3.8/site-packages/qiskit/optimization/problems/quadratic_program.py in substitute_variables(self, constants, variables)
856                 - Coefficient of variable substitution is zero.
857         """
--> 858         return SubstituteVariables().substitute_variables(self, constants, variables)
859
860

/tmp/docs_build/lib/python3.8/site-packages/qiskit/optimization/problems/quadratic_program.py in substitute_variables(self, src, constants, variables)
901         self._src = src
902         self._dst = QuadraticProgram(src.name)
--> 903         self._subs_dict(constants, variables)
904         results = [
905             self._variables(),

/tmp/docs_build/lib/python3.8/site-packages/qiskit/optimization/problems/quadratic_program.py in _subs_dict(self, constants, variables)
960                 j_2 = self._src.get_variable(j).name
961                 if i_2 == j_2:
--> 962                     raise QiskitOptimizationError(
963                         'Cannot substitute the same variable: {} <- {} {}'.format(i, j, v))
964                 if i_2 in subs:

QiskitOptimizationError: 'Cannot substitute the same variable: 0 <- 0 -1'
QiskitOptimizationError: 'Cannot substitute the same variable: 0 <- 0 -1'

# Fixed code
def solve(self, problem: QuadraticProgram) -> MinimumEigenOptimizerResult:
    """Tries to solves the given problem using the optimizer.

    Runs the optimizer to try to solve the optimization problem.

    Args:
        problem: The problem to be solved.

    Returns:
        The result of the optimizer applied to the problem.

    Raises:
        QiskitOptimizationError: If problem not compatible.
    """
    # check compatibility and raise exception if incompatible
    msg = self.get_compatibility_msg(problem)
    if len(msg) > 0:
        raise QiskitOptimizationError("Incompatible problem: {}".format(msg))

    # convert problem to QUBO
    qubo_converter = QuadraticProgramToQubo()
    problem_ = qubo_converter.encode(problem)

    # construct operator and offset
    operator_converter = QuadraticProgramToIsing()
    operator, offset = operator_converter.encode(problem_)

    # only try to solve non-empty Ising Hamiltonians
    if operator.num_qubits > 0:
        # approximate ground state of operator using min eigen solver
        eigen_results = self._min_eigen_solver.compute_minimum_eigenvalue(operator)

        # analyze results
        samples = eigenvector_to_solutions(eigen_results.eigenstate, operator)
        samples = [
            (res[0], problem_.objective.sense.value * (res[1] + offset), res[2])
            for res in samples
        ]
        samples.sort(key=lambda x: problem_.objective.sense.value * x[1])
        x = samples[0][0]
        fval = samples[0][1]

    # if Hamiltonian is empty, then the objective function is constant to the offset
    else:
        x = [0] * problem_.get_num_binary_vars()
        fval = offset
        x_str = "0" * problem_.get_num_binary_vars()
        samples = [(x_str, offset, 1.0)]

    # translate result back to integers
    opt_res = MinimumEigenOptimizerResult(x, fval, samples, qubo_converter)
    opt_res = qubo_converter.decode(opt_res)

    # translate results back to original problem
    return opt_res