I am using default.mixed device but getting the following error
pennylane._device.DeviceError: Gate C(CNOT) not supported on device default.mixed
Is there any way to tackle this?
My code is the following:
import pennylane as qml
from pennylane import numpy as np
import math
# Generate density matrices and arbitrary labels
density_matrix = []
labels = []
for i in range(20):
state = np.random.randn(16,1) + 1j*np.random.randn(16, 1)
state_normlized = np.array(state/np.linalg.norm(state))
density_matrix.append(np.dot(state_normlized, state_normlized.conj().T))
if state_normlized[0].real > 0:
labels.append(1)
else:
labels.append(0)
def cross_entropy(labels, predictions):
loss = 0
for l, p in zip(labels, predictions):
c_entropy = l * (anp.log(p[l])) + (1 - l) * anp.log(1 - p[1 - l])
loss = loss + c_entropy
return -1 * loss
def cost(params, X, Y, cost_fn):
predictions = np.array([circuit(x, params) for x in X])
if cost_fn == 'cross_entropy':
loss = cross_entropy(Y, predictions)
return loss
dev = qml.device('default.mixed', wires = 7)
@qml.qnode(dev)
def circuit(features, params):
ancilla_state = [1/math.sqrt(6), 1/math.sqrt(6), 1/math.sqrt(6), 1/math.sqrt(6), 1/math.sqrt(6), 1/math.sqrt(6), 0, 0]
# Embedding of data and initialization of control register
qml.QubitDensityMatrix(features, wires = [0, 1, 2, 3])
qml.AmplitudeEmbedding(ancilla_state, wires = [4, 5, 6])
qml.ctrl(qml.IsingXX, control=(4,5,6), control_values = (0, 0, 0))(params[0], wires=(0,1))
qml.ctrl(qml.IsingXX, control=(4,5,6), control_values = (0, 0, 1))(params[1], wires=(0,2))
qml.ctrl(qml.IsingXX, control=(4,5,6), control_values = (0, 1, 0))(params[2], wires=(0,3))
qml.ctrl(qml.IsingXX, control=(4,5,6), control_values = (0, 1, 1))(params[3], wires=(1,2))
qml.ctrl(qml.IsingXX, control=(4,5,6), control_values = (1, 0, 0))(params[4], wires=(1,3))
qml.ctrl(qml.IsingXX, control=(4,5,6), control_values = (1, 0, 1))(params[5], wires=(2,3))
result = qml.probs(wires=0)
return result
batch_size = 6
learning_rate = 0.005
total_params = 6
params = np.random.randn(total_params, requires_grad=True)
opt = qml.NesterovMomentumOptimizer(stepsize=learning_rate)
cost_fn = "cross_entropy"
for it in range(10):
batch_index = np.random.randint(0, len(density_matrix), (batch_size,))
X_batch = [density_matrix[i] for i in batch_index]
Y_batch = [labels[i] for i in batch_index]
params, cost_new = opt.step_and_cost(lambda v: cost(v, X_batch, Y_batch, cost_fn), params)
I get the following error message:
File “/home/sreetamadas/anaconda3/lib/python3.10/site-packages/pennylane/qnode.py”, line 1027, in call
res = qml.execute(
File “/home/sreetamadas/anaconda3/lib/python3.10/site-packages/pennylane/interfaces/execution.py”, line 616, in execute
results = inner_execute(tapes)
File “/home/sreetamadas/anaconda3/lib/python3.10/site-packages/pennylane/interfaces/execution.py”, line 249, in inner_execute
return cached_device_execution(tapes)
File “/home/sreetamadas/anaconda3/lib/python3.10/site-packages/pennylane/interfaces/execution.py”, line 371, in wrapper
res = list(fn(tuple(execution_tapes.values()), **kwargs))
File “/home/sreetamadas/anaconda3/lib/python3.10/contextlib.py”, line 79, in inner
return func(*args, **kwds)
File “/home/sreetamadas/anaconda3/lib/python3.10/site-packages/pennylane/_qubit_device.py”, line 460, in batch_execute
res = self.execute(circuit)
File “/home/sreetamadas/anaconda3/lib/python3.10/site-packages/pennylane/devices/default_mixed.py”, line 685, in execute
return super().execute(circuit, **kwargs)
File “/home/sreetamadas/anaconda3/lib/python3.10/site-packages/pennylane/_qubit_device.py”, line 276, in execute
self.check_validity(circuit.operations, circuit.observables)
File “/home/sreetamadas/anaconda3/lib/python3.10/site-packages/pennylane/_device.py”, line 984, in check_validity
raise DeviceError(
pennylane._device.DeviceError: Gate C(CNOT) not supported on device default.mixed