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

add LinearAncillaComposite #530

Merged
merged 9 commits into from
Nov 29, 2024
Merged

add LinearAncillaComposite #530

merged 9 commits into from
Nov 29, 2024

Conversation

pau557
Copy link
Contributor

@pau557 pau557 commented Aug 15, 2024

In scenarios where the h biases are not available or sufficient in range, one can use auxiliary qubits polarized with a large flux bias and coupled to the data qubit. This method:

  • Requires that each qubit is coupled to a non-programmed qubit
  • But, ancilla qubits can be shared between data qubits
  • The magnitude of the bias is limited by the magnitude of the coupler, but in the case of asymetric coupling range, one can use the strongest coupling available and control the bias sign with the flux bias sign

This PR adds a LinearAncillaComposite that implements this technique.

For the reviewers, please take into account future extensions of this method:

  • One can use multiple ancilla qubits to extend the h-range and avoid shrinking energy scales of the problem. This is relevant for QUBO --> Ising translation.
  • While the current implementation may work on embedded problems as is, we need an embedding-aware version that relaxes the condition of each qubit being coupled to an ancilla to each chain being coupled to at least one ancilla.

Please educate me on how to use the correct Sphynx syntax

Copy link

codecov bot commented Aug 15, 2024

Codecov Report

Attention: Patch coverage is 83.33333% with 12 lines in your changes missing coverage. Please review.

Project coverage is 86.57%. Comparing base (b233941) to head (5867eba).
Report is 31 commits behind head on master.

Files with missing lines Patch % Lines
dwave/system/composites/linear_ancilla.py 83.09% 12 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #530      +/-   ##
==========================================
- Coverage   89.65%   86.57%   -3.09%     
==========================================
  Files          24       25       +1     
  Lines        1760     1832      +72     
==========================================
+ Hits         1578     1586       +8     
- Misses        182      246      +64     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.


🚨 Try these New Features:

Copy link
Contributor

@JoelPasvolsky JoelPasvolsky left a comment

Choose a reason for hiding this comment

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

This can be very useful, @pau557. I made a few comments inline.

Needs to be added to docs here: https://github.com/dwavesystems/dwave-system/blob/master/docs/reference/composites.rst

dwave/system/composites/linear_ancilla.py Outdated Show resolved Hide resolved
dwave/system/composites/linear_ancilla.py Outdated Show resolved Hide resolved
dwave/system/composites/linear_ancilla.py Outdated Show resolved Hide resolved
dwave/system/composites/linear_ancilla.py Show resolved Hide resolved
dwave/system/composites/linear_ancilla.py Outdated Show resolved Hide resolved
dwave/system/composites/linear_ancilla.py Show resolved Hide resolved
dwave/system/composites/linear_ancilla.py Show resolved Hide resolved
dwave/system/composites/linear_ancilla.py Outdated Show resolved Hide resolved
dwave/system/composites/linear_ancilla.py Show resolved Hide resolved
dwave/system/composites/linear_ancilla.py Outdated Show resolved Hide resolved
dwave/system/composites/linear_ancilla.py Outdated Show resolved Hide resolved
dwave/system/composites/linear_ancilla.py Outdated Show resolved Hide resolved
dwave/system/composites/linear_ancilla.py Outdated Show resolved Hide resolved
tests/test_linear_ancilla_composite.py Outdated Show resolved Hide resolved
dwave/system/composites/linear_ancilla.py Outdated Show resolved Hide resolved
dwave/system/composites/linear_ancilla.py Outdated Show resolved Hide resolved
dwave/system/composites/linear_ancilla.py Show resolved Hide resolved
dwave/system/composites/linear_ancilla.py Outdated Show resolved Hide resolved
tests/test_linear_ancilla_composite.py Outdated Show resolved Hide resolved
Copy link
Contributor

@JoelPasvolsky JoelPasvolsky left a comment

Choose a reason for hiding this comment

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

Thank you

docs/reference/composites.rst Outdated Show resolved Hide resolved
docs/reference/composites.rst Outdated Show resolved Hide resolved
@pau557
Copy link
Contributor Author

pau557 commented Nov 19, 2024

Any more comments or ready to merge?

@JoelPasvolsky
Copy link
Contributor

Any more comments or ready to merge?

@pau557 there are comments I think you might have missed, especially ones that use GitHub Suggested Change that have not been added. Examples: 1, 2, 3, 4, 5, etc. Can you look through the comments again?

@pau557
Copy link
Contributor Author

pau557 commented Nov 22, 2024

I had missed many comments, apologies @JoelPasvolsky. I think everything is addressed now

Copy link
Contributor

@JoelPasvolsky JoelPasvolsky left a comment

Choose a reason for hiding this comment

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

Just a few small comments.

dwave/system/composites/linear_ancilla.py Outdated Show resolved Hide resolved
dwave/system/composites/linear_ancilla.py Outdated Show resolved Hide resolved
dwave/system/composites/linear_ancilla.py Show resolved Hide resolved
Copy link
Member

@randomir randomir left a comment

Choose a reason for hiding this comment

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

LGTM

bqm: dimod.BinaryQuadraticModel,
*,
h_tolerance: numbers.Number = 0,
default_flux_bias_range: Sequence[float] = (-0.005, 0.005),
Copy link
Member

Choose a reason for hiding this comment

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

why not be more precise with:

Suggested change
default_flux_bias_range: Sequence[float] = (-0.005, 0.005),
default_flux_bias_range: tuple[float, float] = (-0.005, 0.005),

Copy link
Contributor Author

@pau557 pau557 Nov 27, 2024

Choose a reason for hiding this comment

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

CircleCI didn't like that on python 3.8...

  File "/home/circleci/project/dwave/system/composites/linear_ancilla.py", line 96, in LinearAncillaComposite
    default_flux_bias_range: tuple[float, float] = (-0.005, 0.005),
TypeError: 'type' object is not subscriptable

Copy link
Member

Choose a reason for hiding this comment

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

Yup. But we dropped support for 3.8 in the main branch since you opened this PR.. So, best to rebase to master.

Copy link
Member

Choose a reason for hiding this comment

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

@pau557, would you prefer to rebase, or should I do it?

dwave/system/composites/linear_ancilla.py Outdated Show resolved Hide resolved
@randomir
Copy link
Member

@pau557, I think we can merge this when docs comments are addressed.

@pau557
Copy link
Contributor Author

pau557 commented Nov 27, 2024

How do I tell CircleCI not to run this example with a mock sampler?

/home/circleci/project/dwave/system/testing.py:325: UserWarning: 'flux_biases' parameter is valid for DWaveSampler(), but not mocked in MockDWaveSampler().
  warnings.warn(f'{kw!r} parameter is valid for DWaveSampler(), '
/home/circleci/project/dwave/system/testing.py:325: UserWarning: 'fast_anneal' parameter is valid for DWaveSampler(), but not mocked in MockDWaveSampler().
  warnings.warn(f'{kw!r} parameter is valid for DWaveSampler(), '
WARNING: **********************************************************************
File "../dwave/system/composites/linear_ancilla.py", line ?, in default
Failed example:
    sampleset.first.energy
Expected:
    -3
Got:
    1.0

@jackraymond
Copy link
Contributor

jackraymond commented Nov 27, 2024

How do I tell CircleCI not to run this example with a mock sampler?

/home/circleci/project/dwave/system/testing.py:325: UserWarning: 'flux_biases' parameter is valid for DWaveSampler(), but not mocked in MockDWaveSampler().
  warnings.warn(f'{kw!r} parameter is valid for DWaveSampler(), '
/home/circleci/project/dwave/system/testing.py:325: UserWarning: 'fast_anneal' parameter is valid for DWaveSampler(), but not mocked in MockDWaveSampler().
  warnings.warn(f'{kw!r} parameter is valid for DWaveSampler(), '
WARNING: **********************************************************************
File "../dwave/system/composites/linear_ancilla.py", line ?, in default
Failed example:
    sampleset.first.energy
Expected:
    -3
Got:
    1.0

You can mock the flux_biases parameter with h if you want this to behave correctly.
You can also suppress the warnings. e.g.
https://github.com/AndyZzzZzzZzz/shimming-tutorial/blob/development/tutorial_code/helpers/sampler_wrapper.py
I think its worth testing

Copy link
Contributor

@jackraymond jackraymond left a comment

Choose a reason for hiding this comment

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

Couple of suggestions, these needn't be blockers to integration.

flux_biases = [0] * qpu_properties["num_qubits"]

_bqm = bqm.copy()
used_ancillas = defaultdict(list)
Copy link
Contributor

Choose a reason for hiding this comment

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

In practice we may want to share ancillas. This might allow some problems to be programmed that are impossible with unique ancilla per variable. Might be worth adding a comment to this effect somewhere, with a view to a future feature expansion (probably an optional argument).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

import networkx as nx
import numpy as np

from dwave.system.testing import MockDWaveSampler
Copy link
Contributor

Choose a reason for hiding this comment

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

The MockSampler can be amended so as to emulate flux_biases as Ising model linear biases:
https://github.com/AndyZzzZzzZzz/shimming-tutorial/blob/andy/tutorial_code/helpers/sampler_wrapper.py

I recommend you use a wrapper like this one and then test optimization using MockSampler().sample

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The tests in test_linear_ancilla_composite do not check for sample correctness. Just for the implementation of ancillas and flux biases in the call. So the MockSampler works for it.

The error that I posted yesterday was related to circle CI testing the example in a docstring. In that case I am not sure it's possible to wrap with this fancier mock sampler. If it's not trivial to do so I would skip

@JoelPasvolsky
Copy link
Contributor

JoelPasvolsky commented Nov 29, 2024

Looks like an incorrect rebase: did you miss pulling the latest master branch?

@pau557
Copy link
Contributor Author

pau557 commented Nov 29, 2024

Looks like an incorrect rebase: did you miss pulling the latest master branch?

Yes, I did it wrong. @randomir is helping with it.

@randomir
Copy link
Member

Because this PR was opened from a fork's master branch, rebasing would be messy, i.e. history would be pretty much lost.

In order to keep the conversation here relevant (and retain @pau557's authorship of commits), I have rebased in a new branch and verified all tests pass in a separate draft PR, #546. So we can merge this as-is.

@randomir randomir merged commit 5d64d83 into dwavesystems:master Nov 29, 2024
13 of 16 checks passed
@randomir randomir mentioned this pull request Dec 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants