question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] No type safety when passing argument to @qml.device() decorated function

See original GitHub issue

Expected behavior

It would be really nice if I could combine circuits in an obvious way, but the lack of type safety prevents me from doing so whenever the function is decorated with @qml.qnode(device). I really simple example is preparing a VQE circuit from a UCCSD ansatz plus grouped measurement.

I understand that using the built-in methods should take care of most of this for me, but for the purposes of what I am trying to do that simply won’t do.

As a simple example, consider H2 with qubit-wise commuting groups.

import pennylane as qml
from pennylane import qchem
import numpy as np
from functools import partial
from pennylane.templates import UCCSD

#MOLECULE
symbols = ["H", "H"]
coordinates = np.array([0.0, 0.0, -0.6614, 0.0, 0.0, 0.6614])
h2_ham, n_qubits = qchem.molecular_hamiltonian(symbols, coordinates)
n_electrons = 2

groupings, coeffs = qml.grouping.group_observables(h2_ham.terms[1], h2_ham.terms[0], grouping_type = 'qwc')
dev_noisy = qml.device('default.mixed', wires = n_qubits, shots = 1000)

#prepare UCCSD ansatz
singles, doubles = qchem.excitations(n_electrons, n_qubits)
s_wires, d_wires = qchem.excitations_to_wires(singles, doubles)
ref_state = qchem.hf_state(n_electrons, n_qubits)
ansatz = partial(UCCSD, init_state = ref_state, s_wires = s_wires, d_wires = d_wires)

@qml.qnode(dev_noisy)
def VQE_circuit(params, group, n_qubits):
    print(type(group))
    qml.apply(ansatz(params, wires = range(n_qubits)))
    #FAILS HERE, tensor has no attribute wire
    rotations = qml.grouping.diagonalize_qwc_pauli_words(group)[0]
    for rotation in rotations:
        qml.apply(rotation)
    return qml.sample(wires = range(n_qubits))
params = np.random.randn(len(s_wires) + len(d_wires))

circuits = [VQE_circuit(params, group, n_qubits) for group in groupings]

Actual behavior

However, the lack of type safety prevents me from doing so.

Prior to passing group to VQE_circuit, type(group) = <class 'list'> with a particular object being something like: [PauliY(wires=[0]) @ PauliY(wires=[1]) @ PauliX(wires=[2]) @ PauliX(wires=[3])].

However, once it’s in the body of the function: type(group) = <class 'pennylane.numpy.tensor.tensor'> which poses a problem since that class doesn’t interface with diagonalize_qwc_pauli_words bc pennylane.numpy.tensor.tensor objects don’t have wires.

Note that this all works perfectly fine once the decorator @qml.device is removed.

Additional information

No response

Source code

No response

Tracebacks

No response

System information

WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.
Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.
To avoid this problem you can invoke Python with '-m pip' instead of running pip directly.
Name: PennyLane
Version: 0.17.0
Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc.
Home-page: https://github.com/XanaduAI/pennylane
Author: 
Author-email: 
License: Apache License 2.0
Location: /home/kharazi/.local/lib/python3.9/site-packages
Requires: networkx, toml, semantic-version, appdirs, autograd, scipy, autoray, numpy
Required-by: VQELib, PennyLane-Qchem
Platform info:           Linux-5.11.0-34-generic-x86_64-with-glibc2.33
Python version:          3.9.5
Numpy version:           1.21.2
Scipy version:           1.7.1
Installed devices:
- default.gaussian (PennyLane-0.17.0)
- default.mixed (PennyLane-0.17.0)
- default.qubit (PennyLane-0.17.0)
- default.qubit.autograd (PennyLane-0.17.0)
- default.qubit.jax (PennyLane-0.17.0)
- default.qubit.tf (PennyLane-0.17.0)
- default.tensor (PennyLane-0.17.0)
- default.tensor.tf (PennyLane-0.17.0)

  • I have searched exisisting GitHub issues to make sure the issue does not already exist.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
josh146commented, Sep 22, 2021

No worries @kharazity! Since this is a duplicate of an existing bug, I will close this in favour of #1459. However, don’t hesitate to open a new issue if you discover any other bugs!

In addition, we also have a PennyLane discussion forum, where you can ask general code and QML related questions: https://discuss.pennylane.ai

0reactions
josh146commented, Sep 23, 2021

@kharazity I’ve opened a PR, #1680, to fix this latter bug 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

WireError being thrown whenever running circuits where wire ...
I have recently run a clean install of Pennylane in a new environment, and now when I run circuits where the wires are...
Read more >
Accessing properties of a C++ object passed as signal ...
My goal is to pass an instance of the custom C++ class “Contact” as a parameter “contact” of a signal “contactFound” emitted from...
Read more >
WebEngineView QML Type | Qt WebEngine 6.4.1
The newWindowRequested() signal is emitted when a request to load the page in a separate web engine view is issued. The NewViewDestination property...
Read more >
Bug listing with status RESOLVED with resolution TEST ...
Bug :233 - "Emacs segfaults when merged through the sandbox." status:RESOLVED resolution:TEST-REQUEST severity:critical · Bug:3888 - "yenta_socket module not ...
Read more >
Release 0.17.0 Tech Team @ Unitary Fund - Mitiq
The function must be annotated to tell Mitiq which type of ... Note: The atomic_converter() decorator makes it so ... dev = qml.device(....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found