error in quantumCircuit Function?
2 views (last 30 days)
Show older comments
I have tried example in this link
I try to deploy a Quantum Neural Network. But the class PCQ layer always erorrs. I do everything same like in example and here is the class
classdef PQCLayer < nnet.layer.Layer
% Custom PQC layer example.
properties (Learnable)
% Define layer learnable parameters.
A
B
end
methods
function layer = PQCLayer
% Set layer name.
layer.Name = "PQC";
% Set layer description.
layer.Description = "Layer containing a parameterized " + ...
"quantum circuit (PQC)";
% Initialize learnable parameter.
layer.A = 1;
layer.B = 2;
end
function Z = predict(layer,X)
% Z = predict(layer,X) forwards the input data X through the
% layer and outputs the result Z at prediction time.
Z = computeZ(X,layer.A,layer.B);
end
function [dLdX,dLdA,dLdB] = backward(layer,X,Z,dLdZ,memory)
% Backpropagate the derivative of the loss
% function through the layer.
%
% Inputs:
% layer - Layer though which data backpropagates
% X - Layer input data
% Z - Layer output data
% dLdZ - Derivative of loss with respect to layer
% output
% memory - Memory value from forward function
% Outputs:
% dLdX - Derivative of loss with respect to layer input
% dLdA - Derivative of loss with respect to learnable
% parameter A
% dLdB - Derivative of loss with respect to learnable
% parameter B
s = pi/4;
ZPlus = computeZ(X,layer.A + s,layer.B);
ZMinus = computeZ(X,layer.A - s,layer.B);
dZdA = X(1,:).*((ZPlus - ZMinus)./(2*sin(X(1,:).*s)));
dLdA = sum(dLdZ.*dZdA,"all");
ZPlus = computeZ(X,layer.A,layer.B + s);
ZMinus = computeZ(X,layer.A,layer.B - s);
dZdB = X(2,:).*(((ZPlus - ZMinus)./(2*sin(X(2,:).*s))));
dLdB = sum(dLdZ.*dZdB,"all");
% Set the gradients with respect to x and y to zero
% because the QNN does not use them during training.
dLdX = zeros(size(X),"like",X);
end
end
end
function Z = computeZ(X, A, B)
numSamples = size(X,2);
x1 = X(1,:);
x2 = X(2,:);
Z = zeros(1,numSamples,"like",X);
for i = 1:numSamples
circ = quantumCircuit(2);
circ.Gates = [rxGate(1,x1(i)*A); rxGate(2,x2(i)*B); cxGate(1,2)];
s = simulate(circ);
Z(i) = probability(s,2,"0") - probability(s,2,"1");
end
end
But here is the problem. Everything is okay but quantumCircuit doesnt work.
Error using trainNetwork (line 191)
Invalid network.
Caused by:
Layer 'PQC': Error using the predict function in layer PQCLayer. The
function threw an error and could not be executed.
Undefined function 'quantumCircuit' for input arguments of type
'double'.
Error in PQCLayer>computeZ (line 72)
circ = quantumCircuit(2);
Error in PQCLayer/predict (line 27)
Z = computeZ(X,layer.A,layer.B);
Does anybody knows why i get this error?
I changed parameters of the quantumCircuit like
gates = [hGate(1); cxGate(1,2)];
c = quantumCircuit(gates);
but this time i get hGate unrecognized function error. I use Matlab 2024a why i get hese errors?
0 Comments
Accepted Answer
Garmit Pant
on 5 Aug 2024
Edited: Garmit Pant
on 5 Aug 2024
Hello Arda
I gather that you are trying to train and deploy a quantum neural network using MATLAB. To deploy a quantum neural network in MATLAB, you need to install the “MATLAB Support Package for Quantum Computing”.
The support package is required to run the “quantumCircuit” and “hGate” functions. The errors are resolved once the support package is installed.
gates = [hGate(1); cxGate(1,2)];
c = quantumCircuit(gates);
plot(c)
You can install the “MATLAB Support Package for Quantum Computing” using the “Add-On Explorer” or using the following link:
I hope you find the above explanation and suggestions useful!
More Answers (0)
See Also
Categories
Find more on Gate-Based Quantum Computing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!