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

Instr #46

Merged
merged 13 commits into from
Nov 15, 2024
Merged

Instr #46

merged 13 commits into from
Nov 15, 2024

Conversation

stroblme
Copy link
Member

@stroblme stroblme commented Nov 14, 2024

This PR tries to fix the adam issue by globalizing the instructor..

**Depends on #45 **

Melvin Strobl added 3 commits November 14, 2024 16:40
Signed-off-by: Melvin Strobl <[email protected]>
Signed-off-by: Melvin Strobl <[email protected]>
Signed-off-by: Melvin Strobl <[email protected]>
@pep8speaks
Copy link

pep8speaks commented Nov 14, 2024

Hello @stroblme! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

Line 420:9: W503 line break before binary operator
Line 421:9: W503 line break before binary operator
Line 422:9: W503 line break before binary operator
Line 423:9: W503 line break before binary operator
Line 424:9: W503 line break before binary operator
Line 425:9: W503 line break before binary operator

Comment last updated at 2024-11-15 12:52:37 UTC

Melvin Strobl added 5 commits November 14, 2024 23:01
Signed-off-by: Melvin Strobl <[email protected]>
Signed-off-by: Melvin Strobl <[email protected]>
Signed-off-by: Melvin Strobl <[email protected]>
Signed-off-by: Melvin Strobl <[email protected]>
Signed-off-by: Melvin Strobl <[email protected]>
Signed-off-by: Melvin Strobl <[email protected]>

if main_data["number_qubits"] > 1:
instructor.model.params = instructor.model.params
ent_cap = instructor.meyer_wallach(
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think there might be an issue with qml-essentials here, when setting the n_qubits > 1. I get the following stack trace (when removing the try-catch), when resetting and starting the training (e.g. Ansatz=Circuit19, n_qubits=2, n_layers=2):

Traceback (most recent call last):
  File "<path>/mind-the-qapp/.venv/lib/python3.11/site-packages/flask/app.py", line 1473, in wsgi_app
    response = self.full_dispatch_request()
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<path>/mind-the-qapp/.venv/lib/python3.11/site-packages/flask/app.py", line 882, in full_dispatch_request
    rv = self.handle_user_exception(e)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<path>/mind-the-qapp/.venv/lib/python3.11/site-packages/flask/app.py", line 880, in full_dispatch_request
    rv = self.dispatch_request()
         ^^^^^^^^^^^^^^^^^^^^^^^
  File "<path>/mind-the-qapp/.venv/lib/python3.11/site-packages/flask/app.py", line 865, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)  # type: ignore[no-any-return]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<path>/mind-the-qapp/.venv/lib/python3.11/site-packages/dash/dash.py", line 1376, in dispatch
    ctx.run(
  File "<path>/mind-the-qapp/.venv/lib/python3.11/site-packages/dash/_callback.py", line 507, in add_context
    raise err
  File "<path>/mind-the-qapp/.venv/lib/python3.11/site-packages/dash/_callback.py", line 496, in add_context
    output_value = _invoke_callback(func, *func_args, **func_kwargs)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<path>/mind-the-qapp/.venv/lib/python3.11/site-packages/dash/_callback.py", line 43, in _invoke_callback
    return func(*args, **kwargs)  # %% callback invoked %%
           ^^^^^^^^^^^^^^^^^^^^^
  File "<path>/mind-the-qapp/app/pages/1-training.py", line 441, in training
    data = instructor.calc_hist(
           ^^^^^^^^^^^^^^^^^^^^^
  File "<path>/mind-the-qapp/app/utils/instructor.py", line 166, in calc_hist
    .sample_coefficients(self.model, noise_params=noise_params, cache=False)
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<path>/mind-the-qapp/.venv/lib/python3.11/site-packages/qml_essentials/coefficients.py", line 30, in sample_coefficients
    coeffs = coefficients(partial_circuit, 1, model.degree)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<path>/mind-the-qapp/.venv/lib/python3.11/site-packages/pennylane/fourier/coefficients.py", line 178, in coefficients
    return _coefficients_no_filter(f, degree, use_broadcasting)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<path>/mind-the-qapp/.venv/lib/python3.11/site-packages/pennylane/fourier/coefficients.py", line 262, in _coefficients_no_filter
    f_out = f(sampling_point)
            ^^^^^^^^^^^^^^^^^
  File "<path>/mind-the-qapp/.venv/lib/python3.11/site-packages/qml_essentials/model.py", line 483, in __call__
    return self._forward(
           ^^^^^^^^^^^^^^
  File "<path>/mind-the-qapp/.venv/lib/python3.11/site-packages/qml_essentials/model.py", line 616, in _forward
    if len(result.shape) == 3 and result.shape[0] == 1:
           ^^^^^^^^^^^^
AttributeError: 'list' object has no attribute 'shape'

My guess is that this is somehow caused by implicitly setting the execution_type to density when comuting the Meyer-Wallach measure.

Copy link
Member Author

Choose a reason for hiding this comment

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

thanks! already addressed with cirKITers/qml-essentials/pull/61

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah whoops

Copy link
Collaborator

Choose a reason for hiding this comment

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

Updating qml-essentials version to 0.1.17 with 1acfbf0 does not fully solve this problem.
Now getting a different stack trace (probably related to implicitly setting the execution_type):

ERROR:app:Exception on /_dash-update-component [POST]
TypeError: only length-1 arrays can be converted to Python scalars

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<path>/mind-the-qapp/.venv/lib/python3.12/site-packages/flask/app.py", line 1473, in wsgi_app
    response = self.full_dispatch_request()
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<path>/mind-the-qapp/.venv/lib/python3.12/site-packages/flask/app.py", line 882, in full_dispatch_request
    rv = self.handle_user_exception(e)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<path>/mind-the-qapp/.venv/lib/python3.12/site-packages/flask/app.py", line 880, in full_dispatch_request
    rv = self.dispatch_request()
         ^^^^^^^^^^^^^^^^^^^^^^^
  File "<path>/mind-the-qapp/.venv/lib/python3.12/site-packages/flask/app.py", line 865, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)  # type: ignore[no-any-return]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<path>/mind-the-qapp/.venv/lib/python3.12/site-packages/dash/dash.py", line 1376, in dispatch
    ctx.run(
  File "<path>/mind-the-qapp/.venv/lib/python3.12/site-packages/dash/_callback.py", line 507, in add_context
    raise err
  File "<path>/mind-the-qapp/.venv/lib/python3.12/site-packages/dash/_callback.py", line 496, in add_context
    output_value = _invoke_callback(func, *func_args, **func_kwargs)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<path>/mind-the-qapp/.venv/lib/python3.12/site-packages/dash/_callback.py", line 43, in _invoke_callback
    return func(*args, **kwargs)  # %% callback invoked %%
           ^^^^^^^^^^^^^^^^^^^^^
  File "<path>/mind-the-qapp/app/pages/1-training.py", line 440, in training
    data = instructor.calc_hist(
           ^^^^^^^^^^^^^^^^^^^^^
  File "<path>/mind-the-qapp/app/utils/instructor.py", line 166, in calc_hist
    .sample_coefficients(self.model, noise_params=noise_params, cache=False)
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<path>/mind-the-qapp/.venv/lib/python3.12/site-packages/qml_essentials/coefficients.py", line 30, in sample_coefficients
    coeffs = coefficients(partial_circuit, 1, model.degree)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<path>/mind-the-qapp/.venv/lib/python3.12/site-packages/pennylane/fourier/coefficients.py", line 178, in coefficients
    return _coefficients_no_filter(f, degree, use_broadcasting)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<path>/mind-the-qapp/.venv/lib/python3.12/site-packages/pennylane/fourier/coefficients.py", line 263, in _coefficients_no_filter
    f_discrete[nvec] = f_out if use_broadcasting else np.squeeze(f_out)
    ~~~~~~~~~~^^^^^^
ValueError: setting an array element with a sequence.

Copy link
Collaborator

@majafranz majafranz left a comment

Choose a reason for hiding this comment

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

Great work! Thanks! Should we address the qml-essentials issue before merging?

Copy link
Collaborator

@majafranz majafranz left a comment

Choose a reason for hiding this comment

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

Let's gooo

@stroblme stroblme merged commit 85bfc0e into main Nov 15, 2024
1 check passed
@majafranz majafranz deleted the instr branch November 15, 2024 13:46
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.

3 participants