Error regarding tensor block

I run this tensor networks:

dev_1 = qml.device("default.qubit", wires=8)
@qml.qnode(dev_1)
def ASNet(template_weights):
    qml.TTN(
        wires=range(8),
        n_block_wires=2,
        block=ASymmetricDNAEncoder,
        n_params_block=9,
        template_weights=template_weights)    
    for i in range(7):
        qml.CNOT(wires = [i,7])
        qml.CNOT(wires = [7,i])
    return qml.state()

This is the printed error: Traceback (most recent call last):

  File "/Users/namnguyen/Documents/Research/PhD/EtaNET_V3/clean_code/embeddings/encode.py", line 51, in <module>
    print(ASNet(w_as))
  File "/Users/namnguyen/opt/anaconda3/envs/quanonco/lib/python3.7/site-packages/pennylane/qnode.py", line 585, in __call__
    **self.execute_kwargs,
  File "/Users/namnguyen/opt/anaconda3/envs/quanonco/lib/python3.7/site-packages/pennylane/interfaces/batch/__init__.py", line 342, in execute
    cache_execute(batch_execute, cache, return_tuple=False, expand_fn=expand_fn)(tapes)
  File "/Users/namnguyen/opt/anaconda3/envs/quanonco/lib/python3.7/site-packages/pennylane/interfaces/batch/__init__.py", line 173, in wrapper
    res = fn(execution_tapes.values(), **kwargs)
  File "/Users/namnguyen/opt/anaconda3/envs/quanonco/lib/python3.7/site-packages/pennylane/interfaces/batch/__init__.py", line 124, in fn
    tapes = [expand_fn(tape) for tape in tapes]
  File "/Users/namnguyen/opt/anaconda3/envs/quanonco/lib/python3.7/site-packages/pennylane/interfaces/batch/__init__.py", line 124, in <listcomp>
    tapes = [expand_fn(tape) for tape in tapes]
  File "/Users/namnguyen/opt/anaconda3/envs/quanonco/lib/python3.7/site-packages/pennylane/interfaces/batch/__init__.py", line 325, in <lambda>
    expand_fn = lambda tape: device.expand_fn(tape, max_expansion=max_expansion)
  File "/Users/namnguyen/opt/anaconda3/envs/quanonco/lib/python3.7/site-packages/pennylane/_device.py", line 658, in expand_fn
    return self.default_expand_fn(circuit, max_expansion=max_expansion)
  File "/Users/namnguyen/opt/anaconda3/envs/quanonco/lib/python3.7/site-packages/pennylane/_device.py", line 633, in default_expand_fn
    circuit = circuit.expand(depth=max_expansion, stop_at=self.stopping_condition)
  File "/Users/namnguyen/opt/anaconda3/envs/quanonco/lib/python3.7/site-packages/pennylane/tape/tape.py", line 567, in expand
    self, depth=depth, stop_at=stop_at, expand_measurements=expand_measurements
  File "/Users/namnguyen/opt/anaconda3/envs/quanonco/lib/python3.7/site-packages/pennylane/tape/tape.py", line 198, in expand_tape
    obj = obj.expand()
  File "/Users/namnguyen/opt/anaconda3/envs/quanonco/lib/python3.7/site-packages/pennylane/operation.py", line 1108, in expand
    self.decomposition()
  File "/Users/namnguyen/opt/anaconda3/envs/quanonco/lib/python3.7/site-packages/pennylane/operation.py", line 1002, in decomposition
    *self.parameters, wires=self.wires, **self.hyperparameters
  File "/Users/namnguyen/opt/anaconda3/envs/quanonco/lib/python3.7/site-packages/pennylane/templates/tensornetworks/ttn.py", line 214, in compute_decomposition
    if block.__code__.co_argcount > 2:
AttributeError: 'QNode' object has no attribute '__code__'

Please help :slight_smile:
Thanks alot!

Hey @namnguyen0510! I don’t know what ASymmetricDNAEncoder is. Can you include that in your code block?

Here is this:

dev_1 = qml.device("default.qubit", wires=4)
@qml.qnode(dev_1)
def ASymmetricDNAEncoder(X, div_2 = False):
    qml.Rot(phi =X[0], theta = X[1], omega = X[2], wires = 0)
    qml.Rot(phi =X[1], theta = X[2], omega = X[0], wires = 1)
    qml.Rot(phi =X[2], theta = X[0], omega = X[1], wires = 2)
    for i in range(3):
        qml.CNOT(wires = [i,3])
    for i in range(3):
        qml.CNOT(wires = [3,i])
    return qml.density_matrix(wires = [0,3])

I code an algorithm for DNA encoding using Quantum ansatz. The model works well in single block but the error appears when I try to broadcast the tensor network from this architecture.

Thanks!

A couple things I noticed:

  • ASymmetricDNAEncoder doesn’t need to be a QNode :slight_smile:. It just needs to be a quantum function (see block in the example below)
  • ASymmetricDNAEncoder should contain wires as a positional argument so that TTN can apply it properly. In the documentation for TTN it says this

In general, the block takes D parameters and must have the following signature: unitary(parameter1, parameter2, ... parameterD, wires)

Here is the example I mentioned:

import pennylane as qml
import numpy as np

def block(weights, wires):
    qml.CNOT(wires=[wires[0],wires[1]])
    qml.RY(weights[0], wires=wires[0])
    qml.RY(weights[1], wires=wires[1])

n_wires = 4
n_block_wires = 2
n_params_block = 2
n_blocks = qml.TTN.get_n_blocks(range(n_wires),n_block_wires)
template_weights = [[0.1,-0.3]]*n_blocks

dev= qml.device('default.qubit',wires=range(n_wires))
@qml.qnode(dev)
def circuit(template_weights):
    qml.TTN(range(n_wires),n_block_wires,block, n_params_block, template_weights)
    return qml.expval(qml.PauliZ(wires=n_wires-1))

Hope this helps!

Thank you. Much appreciated!

1 Like

Awesome! Glad this helps :slight_smile: