0
$\begingroup$

Hi I am interested in understanding how to use the neural network functions in Mathematica. I have just started learning hence this is a basic query. The simple example I am looking at is as follows: The purpose of the code provided is to build and train a neural network to predict the discretized rotation angle of a qubit based on binary measurement outcomes. The code essentially forms part of a quantum state estimation or quantum phase estimation procedure, where the rotation angle of the qubit around a specific axis (in this case, the Y-axis) is to be determined from measurements made in another basis (the Z-basis).

The code I have so far is as follows

net = NetChain[{LinearLayer[4], ElementwiseLayer["ReLU"], 
    LinearLayer[numBins], SoftmaxLayer[]}, "Input" -> 1, 
   "Output" -> NetDecoder[{"Class", Range[numBins]}]];

(*Training data*)
angles = RandomReal[{0, Pi}, 10];
probabilities = Cos[angles/2]^2;
measurements = 
  RandomVariate[BernoulliDistribution[#]] & /@ probabilities;
discreteAngles = Ceiling[10*angles/Pi];
trainingData = Transpose[{measurements, discreteAngles}];

(*Training the network*)
trainedNet = 
  NetTrain[net, trainingData, LossFunction -> "CrossEntropyLoss", 
   MaxTrainingRounds -> 1];

Print[trainedNet]

I am having difficulty understanding how to incorporate the CrossEntropyLoss function in the code without error. Can anyone advise on how to do this with a basic explanation of how it works, specifically for this example. Any other tips or references would also be appreciated. Many thanks for any assistance.

$\endgroup$
2
  • 1
    $\begingroup$ I don't think there is such thing as LossFunction->"CrossEntropyLoss" unless you define a port in your network called "CrossEntropyLoss". Your network needs a CrossEntropyLossLayer in there somewhere. Also when you get errors, please add these to the question. $\endgroup$
    – flinty
    Commented May 7 at 9:44
  • 1
    $\begingroup$ First, your data is not in correct format. Use trainingData = Rule @@@ Transpose[{measurements, discreteAngles}]. Second, remove "Input" -> 1 from NetChain. Lastly, look into the documentation for NetTrain: NetTrain will automatically use a CrossEntropyLossLayer object with the correct class encoder. Then the code seems to work. $\endgroup$
    – Domen
    Commented May 7 at 9:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.