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

Fix batch_execute output type for QNodes with multiple outputs #163

Merged
merged 3 commits into from
Dec 2, 2021

Conversation

antalszava
Copy link
Contributor

@antalszava antalszava commented Dec 2, 2021

Context

One of the demonstrations fails due to a change of output type when using the qiskit.aer device.

With the latest batch_execute addition, this device may output results in a different format than other devices (e.g., default.qubit).

from pennylane import numpy as np
import pennylane as qml

dev2 = qml.device("qiskit.aer", wires=4)

@qml.qnode(dev2, interface="torch")
def qnode(params):
    qml.RX(params, wires=0)
    return qml.expval(qml.PauliZ(0)), qml.expval(qml.PauliZ(1)), qml.expval(qml.PauliZ(2))


qnode(0.3)
[tensor(0.9512, dtype=torch.float64),
 tensor(1., dtype=torch.float64),
 tensor(1., dtype=torch.float64)]

whereas it should output

tensor([0.9746, 1.0000, 1.0000], dtype=torch.float64)

The reason why this causes is an error in the demo is that we're using torch.stack in the QNodeCollection class on the results of the underlying QNodes. However, with the latest output type, we may end up having a list in the output, on which torch.stack will fail.

Internally, the issue is a missing conversion to np.array, that has been done previously when using the default execute pipeline.

This can be showcased by comparing the outputs of executing the same tape using the default.qubit and the qiskit.aer devices:

from pennylane import numpy as np
import pennylane as qml

with qml.tape.QuantumTape() as tape:
    qml.PauliX(wires=0)
    qml.expval(qml.PauliZ(wires=0)), qml.expval(qml.PauliZ(wires=1))

dev1 = qml.device("qiskit.aer", wires=4)
print(dev1.batch_execute([tape]))

dev2 = qml.device("default.qubit", wires=4)

print(dev2.batch_execute([tape]))
[[-1.0, 1.0]]
[array([-1.,  1.])]

Changes

Converts the output of QubitDevice.statistics to a numpy array to resolve the issue.

@codecov
Copy link

codecov bot commented Dec 2, 2021

Codecov Report

Merging #163 (5424047) into master (63f2efa) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master      #163   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            7         7           
  Lines          313       314    +1     
=========================================
+ Hits           313       314    +1     
Impacted Files Coverage Δ
pennylane_qiskit/qiskit_device.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 63f2efa...5424047. Read the comment docs.

Copy link
Contributor

@rmoyard rmoyard left a comment

Choose a reason for hiding this comment

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

Thanks @antalszava I tried different cases and it is working! Just left one comment 💯

pennylane_qiskit/qiskit_device.py Show resolved Hide resolved
@antalszava antalszava merged commit 0998b85 into master Dec 2, 2021
@antalszava antalszava deleted the fix_batch_output branch December 2, 2021 20:09
@antalszava
Copy link
Contributor Author

[sc-11994]

rmoyard pushed a commit that referenced this pull request Feb 14, 2022
antalszava added a commit that referenced this pull request Feb 15, 2022
* Small change.

* Update header.html

* Add native support for the SX gate (#158)

* Add SX support

* changelog

* SX inverse tests

* Define the `QiskitDevice.batch_execute` method (#156)

* first draft

* license

* apply; compile in the method

* statevector & batch generate samples

* docstrings

* tests

* batch_params check & check call count for batch_execute

* IBMQ check

* docstrings

* batch execution pipeline test for gradient computation

* IBMQ grad test

* state

* no single tape case; re-add basis state prep to the test

* name and docstring

* renames

* comment

* docstrings, names

* remove unnecessary reset

* Update pennylane_qiskit/qiskit_device.py

Co-authored-by: Romain <[email protected]>

* Update pennylane_qiskit/qiskit_device.py

Co-authored-by: Romain <[email protected]>

Co-authored-by: Romain <[email protected]>

* Update the batch execution tests to use the new QNode (#159)

* batch execute call counter

* get PennyLane master

* comment

* flaky

* Update test_expval.py

* more flaky

Co-authored-by: Romain <[email protected]>

* Change token for IBMQ tests (#161)

* Small change.

* Change token.

* Add env.

* Not device test.

* Batch execute twice

* Add job_time (#160)

* commit

* black

* test

* checking coverage

* reset test

* qasm_simulator

* test

* test

* change

* update

* record

* Romain changes

* Update ibmq.py

* final change

* Update test_ibmq.py

* Update CHANGELOG.md

Co-authored-by: Romain <[email protected]>

* Add the `qml.Identity` operation to PennyLane-Qiskit (#162)

* Add the qml.Identity gate to the Qiskit plugin

* Add Identity to converter test

* Fix pylint:f-string in qiskit_device.py

* Changelog & additional test

* Update CHANGELOG.md

Co-authored-by: Romain <[email protected]>

* Fix `batch_execute` output type for QNodes with multiple outputs (#163)

* asarray

* check type

* changelog

* Update the tracker used with batch execution (#167)

* tracker test

* changelog

* Update tests/test_integration.py

Co-authored-by: Romain <[email protected]>

Co-authored-by: Romain <[email protected]>

* Update header.html

* Python 3.10 (#170)

* changelog & tests

* changelog

* Version bump (#168)

* Version bump
s Please enter the commit message for your changes. Lines starting

* add pl version

Co-authored-by: Josh Izaac <[email protected]>

* v0.21.0-dev version bump (#172)

* inc version

* add flaky

* Improve token-specified login flow. (#169)

* Test that we can create multiple IBMQ devices using an environment token.

* Improve token-specified login flow.

* Fix formatting.

* extend skipping logic

* Test token login elision.

* Temporarily restrict code coverage analysis.

* Update changelog.

* Fix formatting.

* more no cover for the IBMQ device

* Update tests/test_ibmq.py

Co-authored-by: antalszava <[email protected]>

* Update tests/test_ibmq.py

Co-authored-by: antalszava <[email protected]>

* Update test_ibmq.py

Co-authored-by: Antal Szava <[email protected]>

* Change IBMQ login (#175)

* Small change.

* Change test.

* Test.

* Update.

* Change tests.

* Fix bugs for the trainable arguments update (#177)

* Small change.

* Tests

* Exchange.

* Update.

* Update.

* Add circuit-runner, sampler and custom VQE programs from Qiskit-Runtime (#157)

* Small change.

* First commit

* Working circuit runner.

* Sampler

* Sampler

* VQE is working.

* Headers.

* Update for batches.

* Update docstrigs.

* CodeFactor.

* Add tests for runtime.

* Tests.

* Test.

* Test circuit runner.

* Test Sampler.

* Correct generate_samples

* Add entry points.

* More shots.

* Add token arg.

* Change import

* Change Sampler sample.

* Circuit runner kwargs test.

* Shots kwargs

* Change import.

* Add kwargs Sampler test.

* Import.

* Impport again.

* Import change

* Add test tracker.

* Update track tests.

* Update tracker runtime.

* Update tracker.

* Update.

* Codfactor changes.

* Update.

* Update codefactor

* More codefactor.

* Add callback

* Update

* Move vqe

* Update.

* Add test VQE.

* Update.

* delete

* Update VQE test.

* Add shots.

* Update test

* Update delete

* Unused import.

* More iteration.

* m

* Change tol.

* More VQE tests.

* Black

* Update.

* Update.

* Update

* Update pennylane_qiskit/vqe/__init__.py

Co-authored-by: antalszava <[email protected]>

* Update from review.

* Update tests.

* Typo.

* Update from tests.

* Runtime updated.

* Runtime updated.

* Update vqe

* Update

* Update from Antal review.

* Update

* Black.

* More test.

* Update parameters

* Codefactor update.

* Update.

* Update.

* Change order jac.

* Typo.

* Params.

* Update tests.

* ValueError.

* print

* Update.

* Tol.

* Update review.

* Add doc.

* Unused variable.

* Update tests.

* Update params.

* Update review.

* Readd mthree

* Typo.

* Path.

* Black

* Change path.

* Update runtime_programs/vqe_runtime_program.py

* Update doc/devices/runtime.rst

Co-authored-by: antalszava <[email protected]>

* Update review.

* coverarc

* coverarc omit + skip IBMQ and Runtime tests

* Changelog

Co-authored-by: antalszava <[email protected]>

* Add and change some docstrings for a better quality (#174)

* Small change.

* First check.

* Typo.

* Add link.

* Change.

* Change.

* Update pennylane_qiskit/qiskit_device.py

* Update from review.

* Typo

* Update docstring transpiler.

* Update.

Co-authored-by: antalszava <[email protected]>

* Changes.

* Review

* Review

* Wrong changelog number

Co-authored-by: Josh Izaac <[email protected]>
Co-authored-by: antalszava <[email protected]>
Co-authored-by: Guillermo Alonso-Linaje <[email protected]>
Co-authored-by: David Ittah <[email protected]>
Co-authored-by: Jay Soni <[email protected]>
Co-authored-by: Tanner Rogalsky <[email protected]>
antalszava added a commit that referenced this pull request Feb 15, 2022
* changelog and bump version

* Change callback function for Sicpy intermediate results (#187)

* Small change.

* Update header.html

* Add native support for the SX gate (#158)

* Add SX support

* changelog

* SX inverse tests

* Define the `QiskitDevice.batch_execute` method (#156)

* first draft

* license

* apply; compile in the method

* statevector & batch generate samples

* docstrings

* tests

* batch_params check & check call count for batch_execute

* IBMQ check

* docstrings

* batch execution pipeline test for gradient computation

* IBMQ grad test

* state

* no single tape case; re-add basis state prep to the test

* name and docstring

* renames

* comment

* docstrings, names

* remove unnecessary reset

* Update pennylane_qiskit/qiskit_device.py

Co-authored-by: Romain <[email protected]>

* Update pennylane_qiskit/qiskit_device.py

Co-authored-by: Romain <[email protected]>

Co-authored-by: Romain <[email protected]>

* Update the batch execution tests to use the new QNode (#159)

* batch execute call counter

* get PennyLane master

* comment

* flaky

* Update test_expval.py

* more flaky

Co-authored-by: Romain <[email protected]>

* Change token for IBMQ tests (#161)

* Small change.

* Change token.

* Add env.

* Not device test.

* Batch execute twice

* Add job_time (#160)

* commit

* black

* test

* checking coverage

* reset test

* qasm_simulator

* test

* test

* change

* update

* record

* Romain changes

* Update ibmq.py

* final change

* Update test_ibmq.py

* Update CHANGELOG.md

Co-authored-by: Romain <[email protected]>

* Add the `qml.Identity` operation to PennyLane-Qiskit (#162)

* Add the qml.Identity gate to the Qiskit plugin

* Add Identity to converter test

* Fix pylint:f-string in qiskit_device.py

* Changelog & additional test

* Update CHANGELOG.md

Co-authored-by: Romain <[email protected]>

* Fix `batch_execute` output type for QNodes with multiple outputs (#163)

* asarray

* check type

* changelog

* Update the tracker used with batch execution (#167)

* tracker test

* changelog

* Update tests/test_integration.py

Co-authored-by: Romain <[email protected]>

Co-authored-by: Romain <[email protected]>

* Update header.html

* Python 3.10 (#170)

* changelog & tests

* changelog

* Version bump (#168)

* Version bump
s Please enter the commit message for your changes. Lines starting

* add pl version

Co-authored-by: Josh Izaac <[email protected]>

* v0.21.0-dev version bump (#172)

* inc version

* add flaky

* Improve token-specified login flow. (#169)

* Test that we can create multiple IBMQ devices using an environment token.

* Improve token-specified login flow.

* Fix formatting.

* extend skipping logic

* Test token login elision.

* Temporarily restrict code coverage analysis.

* Update changelog.

* Fix formatting.

* more no cover for the IBMQ device

* Update tests/test_ibmq.py

Co-authored-by: antalszava <[email protected]>

* Update tests/test_ibmq.py

Co-authored-by: antalszava <[email protected]>

* Update test_ibmq.py

Co-authored-by: Antal Szava <[email protected]>

* Change IBMQ login (#175)

* Small change.

* Change test.

* Test.

* Update.

* Change tests.

* Fix bugs for the trainable arguments update (#177)

* Small change.

* Tests

* Exchange.

* Update.

* Update.

* Add circuit-runner, sampler and custom VQE programs from Qiskit-Runtime (#157)

* Small change.

* First commit

* Working circuit runner.

* Sampler

* Sampler

* VQE is working.

* Headers.

* Update for batches.

* Update docstrigs.

* CodeFactor.

* Add tests for runtime.

* Tests.

* Test.

* Test circuit runner.

* Test Sampler.

* Correct generate_samples

* Add entry points.

* More shots.

* Add token arg.

* Change import

* Change Sampler sample.

* Circuit runner kwargs test.

* Shots kwargs

* Change import.

* Add kwargs Sampler test.

* Import.

* Impport again.

* Import change

* Add test tracker.

* Update track tests.

* Update tracker runtime.

* Update tracker.

* Update.

* Codfactor changes.

* Update.

* Update codefactor

* More codefactor.

* Add callback

* Update

* Move vqe

* Update.

* Add test VQE.

* Update.

* delete

* Update VQE test.

* Add shots.

* Update test

* Update delete

* Unused import.

* More iteration.

* m

* Change tol.

* More VQE tests.

* Black

* Update.

* Update.

* Update

* Update pennylane_qiskit/vqe/__init__.py

Co-authored-by: antalszava <[email protected]>

* Update from review.

* Update tests.

* Typo.

* Update from tests.

* Runtime updated.

* Runtime updated.

* Update vqe

* Update

* Update from Antal review.

* Update

* Black.

* More test.

* Update parameters

* Codefactor update.

* Update.

* Update.

* Change order jac.

* Typo.

* Params.

* Update tests.

* ValueError.

* print

* Update.

* Tol.

* Update review.

* Add doc.

* Unused variable.

* Update tests.

* Update params.

* Update review.

* Readd mthree

* Typo.

* Path.

* Black

* Change path.

* Update runtime_programs/vqe_runtime_program.py

* Update doc/devices/runtime.rst

Co-authored-by: antalszava <[email protected]>

* Update review.

* coverarc

* coverarc omit + skip IBMQ and Runtime tests

* Changelog

Co-authored-by: antalszava <[email protected]>

* Add and change some docstrings for a better quality (#174)

* Small change.

* First check.

* Typo.

* Add link.

* Change.

* Change.

* Update pennylane_qiskit/qiskit_device.py

* Update from review.

* Typo

* Update docstring transpiler.

* Update.

Co-authored-by: antalszava <[email protected]>

* Changes.

* Review

* Review

* Wrong changelog number

Co-authored-by: Josh Izaac <[email protected]>
Co-authored-by: antalszava <[email protected]>
Co-authored-by: Guillermo Alonso-Linaje <[email protected]>
Co-authored-by: David Ittah <[email protected]>
Co-authored-by: Jay Soni <[email protected]>
Co-authored-by: Tanner Rogalsky <[email protected]>

* Update CHANGELOG.md

Co-authored-by: Romain <[email protected]>
Co-authored-by: Josh Izaac <[email protected]>
Co-authored-by: Guillermo Alonso-Linaje <[email protected]>
Co-authored-by: David Ittah <[email protected]>
Co-authored-by: Jay Soni <[email protected]>
Co-authored-by: Tanner Rogalsky <[email protected]>
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.

2 participants