Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: '_DiagonalEstimator' object has no attribute '_circuits' #157

Closed
CandyPanda-LS opened this issue Feb 17, 2024 · 2 comments
Closed
Labels
bug Something isn't working

Comments

@CandyPanda-LS
Copy link

Environment

  • Qiskit Algorithms version: 0.2.2
  • Python version: 3.10.12
  • Operating system: Google colab (linux)
  • Related packafes: qiskit==1.0.0

What is happening?

I tried to solve the portfolio optimization by defining a custom problem apart from the already available problem. Given below is the equation I want to minimize in the unconstrained form. But when I formulate this as a QP and provide to the optimizer I am getting and error AttributeError: '_DiagonalEstimator' object has no attribute '_circuits' (please find the stack trace attached underneath)

Screenshot 2024-02-17 at 12 08 51
Screenshot 2024-02-17 at 12 09 07

How can we reproduce the issue?

from typing import List, Tuple, Union, Optional

import numpy as np

from docplex.mp.advmodel import AdvModel

from qiskit_optimization.algorithms import OptimizationResult
from qiskit_optimization.applications import OptimizationApplication
from qiskit_optimization.problems import QuadraticProgram
from qiskit_optimization.translators import from_docplex_mp
from qiskit_finance.exceptions import QiskitFinanceError

mdl = AdvModel(name="Portfolio optimization")

N = 10
M = 5

lambda_risk = 0.5
lambda_linking = 0.1
lambda_budget = 0.2
lambda_diversification = 0.15
lambda_cardinality = 0.05

R = 100 # Example: Risk tolerance parameter
B = 50 # Example: Total budget
D = 10 # Example: Diversification requirement
K = 3 # Example: Cardinality constraint (e.g., number of assets to select)

mu = np.random.uniform(low=0.1, high=1.0, size=N)
sigma = np.random.uniform(low=0.01, high=0.1, size=(N, N))
c = np.random.uniform(low=1, high=10, size=N)

z = {(i, l): mdl.binary_var(name=f"z_{i}{l}") for i in range(N) for l in range(M)}
y = [mdl.binary_var(name=f"y{i}") for i in range(N)]

risk_term = mdl.sum(lambda_risk * ((2**(l+p) * sigma[i][j])2 - R * 2(l+p+1) * sigma[i][j]) * z[i, l] * z[j, p]
for i in range(N) for j in range(N) for l in range(M) for p in range(M))

linking_term = mdl.sum(-lambda_linking * (2**(l+1) * D) * z[i, l] * y[i]
for i in range(N) for l in range(M))

other_terms = mdl.sum((lambda_budget * (2**(2l) * c[i]2 - 2(l+1) * c[i] * B) +
lambda_diversification * (2**(2l) - 2**(l+1) * D) +
lambda_linking * 2**(2*l) - mu[i] * 2**l) * z[i, l]
for i in range(N) for l in range(M))

cardinality_term = mdl.sum((lambda_cardinality * (1 - 2 * K) + lambda_linking * D**2) * y[i] for i in range(N))

constant_terms = lambda_risk * R2 + lambda_budget * B2 + lambda_diversification * D2 + lambda_cardinality * K2

mdl.minimize(risk_term + linking_term + other_terms + cardinality_term + constant_terms)

from qiskit_algorithms import NumPyMinimumEigensolver, QAOA, SamplingVQE
from qiskit_optimization.algorithms import MinimumEigenOptimizer
from qiskit_algorithms.optimizers import COBYLA
from qiskit_aer.primitives import Sampler

qp = from_docplex_mp(mdl)
cobyla = COBYLA()
cobyla.set_options(maxiter=250)
qaoa_mes = QAOA(sampler=Sampler(), optimizer=cobyla, reps=3)
qaoa = MinimumEigenOptimizer(qaoa_mes)
result = qaoa.solve(qp)

What should happen?

The modeled optimization problem should be solved.

Any suggestions?

No response

@CandyPanda-LS CandyPanda-LS added the bug Something isn't working label Feb 17, 2024
@woodsp-ibm
Copy link
Member

woodsp-ibm commented Feb 17, 2024

As was surmised in Qiskit/qiskit#11818 this was addressed by #98. There are a couple of such compatibility fixes that have been done to work with the changes that took place in Qiskit 1.0 that are pending. You have a couple of options, if you want things working ahead of a new release of qiskit algorithms which, now that Qiskit 1.0 is out, should be shortly. You can pip install qiskit==0.46 to use the prior version, which is still supported, or you can install qiskit algorithms from the source here; see the docs Getting Started if you need more info on the latter.

FYI the CI for Qiskit Optimization is breaking due to the same issue you are seeing qiskit-community/qiskit-optimization#609. That too, while we can work around it, which is what that PR was to do, and not use 1.0, its really too waiting for the algorithms release with the fix.

@woodsp-ibm
Copy link
Member

Qiskit Algorithms 0.3.0 was released earlier today. If you install that then the problem you note above, when using Optimization with Qiskit 1.0, no longer exists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants