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

Adding support for observables in LRE executors #2681

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

Shivansh20128
Copy link
Contributor

@Shivansh20128 Shivansh20128 commented Mar 1, 2025

This PR aims to add support for observables in the LRE executors.
#Resolves #2513

Description

  1. Added required parameters and code changes in lre.py.
  2. Added tests in the test file to check observable support.

License

  • I license this contribution under the terms of the GNU GPL, version 3 and grant Unitary Fund the right to provide additional permissions as described in section 7 of the GNU GPL, version 3.

Before opening the PR, please ensure you have completed the following where appropriate.

Copy link

codecov bot commented Mar 1, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.67%. Comparing base (34eb6ce) to head (be35300).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2681   +/-   ##
=======================================
  Coverage   98.67%   98.67%           
=======================================
  Files          93       93           
  Lines        4223     4223           
=======================================
  Hits         4167     4167           
  Misses         56       56           

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

@Shivansh20128 Shivansh20128 marked this pull request as ready for review March 1, 2025 10:25
@Shivansh20128
Copy link
Contributor Author

Hi! I tried adding the test for a batched executor as well but could not prepare an executor that would support an observable as discussed here.
If its desirable, can someone suggest how I can proceed.

I was trying something like this:

@pytest.mark.parametrize(
    "degree, fold_multiplier, observable",
    [(2, 2, Observable[PauliString(spec="X")])],
)
def test_lre_batched_executor_with_observable(degree, fold_multiplier, observable):
    """Verify LRE batch executor with observable works as expected."""
    test_batched_executor = Executor(batched_executor, max_batch_size=200)
    lre_exp_val_batched = execute_with_lre(
        test_cirq,
        test_batched_executor,
        degree=degree,
        fold_multiplier=fold_multiplier,
        observable=observable,
    )
    assert isinstance(lre_exp_val_batched, float)

    assert test_batched_executor.calls_to_executor == 1
    assert (
        test_batched_executor.executed_circuits
        == multivariate_layer_scaling(test_cirq, degree, fold_multiplier)
    )

You can find the batched_executor here.

The problem is that I was unable to formulate a batched_executor that supports observables.

@natestemen natestemen requested a review from bdg221 March 7, 2025 17:44
@bdg221
Copy link
Collaborator

bdg221 commented Mar 11, 2025

If you check test_executor.py you can see a lot of examples of executors. I would recommend using executor_density_matrix_typed and executor_density_matrix_batched.
You should then be able to do:

test_batched_executor = Executor(executor_density_matrix_batched, max_batch_size=200)

Copy link
Collaborator

@bdg221 bdg221 left a comment

Choose a reason for hiding this comment

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

@Shivansh20128 Let me know if you have any more questions about the batched executors with observables. Hopefully, the test_exectuor.py examples are helpful.

"degree, fold_multiplier, observable",
[(2, 2, obs_z), (2, 3, obs_y), (3, 4, obs_x)],
)
def test_lre_executor_with_observables(degree, fold_multiplier, observable):
Copy link
Collaborator

Choose a reason for hiding this comment

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

This looks to be the exact same as test_lre_exp_value_with_observable. I would recommend moving the assert lines to that test and removing this one.

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.

Make sure LRE executors work with observables
2 participants