Delivering Correct Wires To QNode

Hi,

I’m trying to pass a list of wires that I want my QNode to operate on to my default QNode. I’ve been trying to do a couple things:

  1. print out the values that I’m passing in to make sure that I’m passing in the correct values
  2. get the values to be recognized in the QNode to define my operations

my code flows something like the following:

dev = qml.device('default.qubit', wires=4)
@qml.qnode(dev, interface='torch')
def QCircuit(wire):    
	#I can't assign the wire values; it just won't let me do it, even when 
        my defined values are integers
	qml.RX(1., wires=wire[0]) 
	qml.RY(2., wires=wire[1])
        qml.RZ(3., wires=wire[2])
	qml.RX(4., wires=wire[3])
	#This is a test to see if I can print out the value- I fail at doing this
        test=wire[0]
        print(test)
        #print(Variable(test).data.numpy())
    return qml.expval(qml.PauliZ(0))
class QuantumCircuitTest(torch.nn.Module):
    def __init__(self, input):
        super(QuantumCircuitTest, self).__init__()
	self.wire = [0,1,2,3]
    def forward(self, input):
	return QCircuit(self.wire)

Printing the above value doesn’t give me anything hopeful at most I get
‘Variable 10: name = None,’
Furthermore I get thrown an error when I try to do ‘wires=wire[0]’

Any ideas or suggestions? At this point I’m stuck.

Hi @QMLHappy!

Because wires are non-differentiable (they only accept integer values), they must be passed as keyword arguments.

Anything passed as a positional argument, PennyLane will assume is differentiable. By passing as a keyword argument, this instructs PennyLane not to attempt to differentiate them.

For example,

dev = qml.device('default.qubit', wires=4)

@qml.qnode(dev, interface='torch')
def QCircuit(wire=None): # keyword argument
    qml.RX(1., wires=wire[0]) 
    qml.RY(2., wires=wire[1])
    qml.RZ(3., wires=wire[2])
    qml.RX(4., wires=wire[3])
    print(wire)
    return qml.expval(qml.PauliZ(0))

class QuantumCircuitTest(torch.nn.Module):
    def __init__(self, input):
        super(QuantumCircuitTest, self).__init__()
        self.wire = [0, 1, 2, 3]

    def forward(self, input):
        return QCircuit(wire=self.wire) #keyword argument

Running this circuit:

>>> qc = QuantumCircuitTest([0])                                                                                                                                                                              
>>> qc([0])                                                                                                                                                                                                   
[0, 1, 2, 3]
tensor(0.5403, dtype=torch.float64)

Note that with a keyword argument, the print statement now also works :slightly_smiling_face:

1 Like