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

(PoC) Add Primitives V2 + ISA circuit support #623

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

t-imamichi
Copy link
Collaborator

@t-imamichi t-imamichi commented May 30, 2024

Summary

Based on #622

This PR is just a PoC for discussions. Not intended to be merged yet.

This PR copies the following necessary modules from qiskit-algorithms to qiskti-optimization.compat module by modifying them to support Primitives V2 + ISA circuits.

  • SamplingVQE
  • QAOA
  • _DiagonalEstimator (private class)

This allows us to use qiskit-optimization with SamplerV2 and a real device as follows.

MinimumEigenOptimizer

from docplex.mp.model import Model
from qiskit.primitives import StatevectorSampler
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit_aer.primitives import Sampler
from qiskit_algorithms.optimizers import COBYLA
from qiskit_ibm_runtime import QiskitRuntimeService, SamplerV2

from qiskit_optimization.algorithms import MinimumEigenOptimizer
from qiskit_optimization.compat import QAOA
from qiskit_optimization.translators import from_docplex_mp

mdl = Model("docplex model")
x = mdl.binary_var("x")
y = mdl.binary_var("y")
mdl.minimize(x - 2 * y)
op = from_docplex_mp(mdl)
opt_sol = -2

optimizer = COBYLA(maxiter=1)

for sampler in [Sampler(), StatevectorSampler()]:
    qaoa = QAOA(sampler, optimizer)
    meo = MinimumEigenOptimizer(qaoa)
    results = meo.solve(op)
    print(sampler)
    print(results)

service = QiskitRuntimeService(instance="...")
backend_name = "..."
backend = service.get_backend(backend_name)
pm = generate_preset_pass_manager(optimization_level=2, target=backend.target)

for sampler in [SamplerV2(backend=backend_name)]:
    qaoa = QAOA(sampler, optimizer, passmanager=pm)
    meo = MinimumEigenOptimizer(qaoa)
    results = meo.solve(op)
    print(sampler)
    print(results)

output

<qiskit_aer.primitives.sampler.Sampler object at 0x11f426c20>
fval=-2.0, x=0.0, y=1.0, status=SUCCESS
<qiskit.primitives.statevector_sampler.StatevectorSampler object at 0x11f4267d0>
fval=-2.0, x=0.0, y=1.0, status=SUCCESS
<qiskit_ibm_runtime.sampler.SamplerV2 object at 0x12fad20e0>
fval=-2.0, x=0.0, y=1.0, status=SUCCESS

grover optimizer

from docplex.mp.model import Model
from qiskit.primitives import StatevectorSampler
from qiskit_aer.primitives import Sampler
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit_ibm_runtime import QiskitRuntimeService, SamplerV2

from qiskit_optimization.algorithms import GroverOptimizer
from qiskit_optimization.translators import from_docplex_mp

mdl = Model("docplex model")
x = mdl.binary_var("x")
y = mdl.binary_var("y")
mdl.minimize(x - 2 * y)
op = from_docplex_mp(mdl)
opt_sol = -2

for sampler in [Sampler(), StatevectorSampler()]:
    grover_optimizer = GroverOptimizer(
        num_value_qubits=3,
        num_iterations=1,
        converters=None,
        sampler=sampler,
    )
    results = grover_optimizer.solve(op)
    print(sampler)
    print(results)

service = QiskitRuntimeService(instance="...")
backend_name = "..."
backend = service.get_backend(backend_name)
pm = generate_preset_pass_manager(optimization_level=2, target=backend.target)

for sampler in [SamplerV2(backend=backend_name)]:
    grover_optimizer = GroverOptimizer(
        num_value_qubits=3,
        num_iterations=1,
        converters=None,
        sampler=sampler,
        passmanager=pm,
    )
    results = grover_optimizer.solve(op)
    print(sampler)
    print(results)

output

<qiskit_aer.primitives.sampler.Sampler object at 0x137516ce0>
fval=-1.0, x=1.0, y=1.0, status=SUCCESS
<qiskit.primitives.statevector_sampler.StatevectorSampler object at 0x137517130>
fval=-1.0, x=1.0, y=1.0, status=SUCCESS
<qiskit_ibm_runtime.sampler.SamplerV2 object at 0x1531bd0c0>
fval=-1.0, x=1.0, y=1.0, status=SUCCESS

Details and comments

This PR is still WIP. It is missing the following items at least.

  • No test
  • No check of pass manager if users use a real device.

@coveralls
Copy link

Pull Request Test Coverage Report for Build 9296831983

Details

  • 42 of 305 (13.77%) changed or added relevant lines in 7 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-4.8%) to 88.118%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit_optimization/algorithms/qrao/magic_rounding.py 17 19 89.47%
qiskit_optimization/compat/init.py 0 3 0.0%
qiskit_optimization/algorithms/qrao/encoding_commutation_verifier.py 14 19 73.68%
qiskit_optimization/compat/qaoa.py 0 21 0.0%
qiskit_optimization/compat/diagonal_estimator.py 0 103 0.0%
qiskit_optimization/compat/sampling_vqe.py 0 129 0.0%
Totals Coverage Status
Change from base Build 9128549330: -4.8%
Covered Lines: 4472
Relevant Lines: 5075

💛 - Coveralls

@MiasWuQG
Copy link
Collaborator

As it was mentioned, the changes rely on the modifications in qiskit_algorithm that you add in compat/. We may have to wait for a similar fix in qiskit_algorithm before we implement this PoC.
Nevertheless, the problem is valid, SamplerV2 and EstimatorV2 can't be used with the QAOA class at the moment, but they work with the additions in compat/ .

@woodsp-ibm
Copy link
Member

As a FYI I'll link this PR

where the parts of qiskit.algorithms (inlcude optimizers) that were in use by ML have been copied over into ML, with algorithms going unsupported.:

https://github.com/qiskit-community/qiskit-algorithms?tab=readme-ov-file#qiskit-algorithms

Warning

Qiskit Algorithms is no longer officially supported by IBM.
Like any other Apache 2 licensed code, you are free to use it or/and extend it, but please be aware that it is under your own risk.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants