0
$\begingroup$

I want to use Q# to evaluate the real resources of an operation, but some problems happen. I want to evaluate the resource of training model operation for the example of half-moons data classification.

enter image description here

The evaluation target is TrainHalMoonModel operaion.

Firstly I use code like the below:

if __name__ == "__main__":
    with open('data.json') as f:
        data = json.load(f)
    parameter_starting_points = [
        [0.060057, 3.00522,  2.03083,  0.63527,  1.03771, 1.27881, 4.10186,  5.34396],
        [0.586514, 3.371623, 0.860791, 2.92517,  1.14616, 2.99776, 2.26505,  5.62137],
        [1.69704,  1.13912,  2.3595,   4.037552, 1.63698, 1.27549, 0.328671, 0.302282],
        [5.21662,  6.04363,  0.224184, 1.53913,  1.64524, 4.79508, 1.49742,  1.545]
     ]     
    trainingVectors=data['TrainingData']['Features'],      
    trainingLabels=data['TrainingData']['Labels'],
    initialParameters=parameter_starting_points
     #qubit_result = TrainHalfMoonModel.estimate_resources(trainingVectors=trainingVectors,trainingLabels=trainingLabels,initialParameters=initialParameters)
    
    qubit_result = TrainHalfMoonModel.estimate_resources()
    print(qubit_result)

The error information shows below:

Received invalid parameters. Please fix and try again: trainingVectors: missing. trainingLabels: missing. initialParameters: missing. Traceback (most recent call last): File "D:\vscode_python\HelloQ\learnQ\machineLearning\evaluate.py", line 30, in qubit_result = TrainHalfMoonModel.estimate_resources() File "D:\software\python\Anaconda3\envs\qsharp-env\lib\site-packages\qsharp\loader.py", line 101, in estimate_resources return qsharp.client.estimate(self, **kwargs) File "D:\software\python\Anaconda3\envs\qsharp-env\lib\site-packages\qsharp\clients\iqsharp.py", line 191, in estimate for row in raw_counts: TypeError: 'NoneType' object is not iterable

The trainingVectors,trainingLabels,initialParameters are exactly parameters of TrainHalfMoonModel,I guess the estimate_resources() method may need parameters in this condition. so I use code like this:

if __name__ == "__main__":
    with open('data.json') as f:
        data = json.load(f)
    parameter_starting_points = [
        [0.060057, 3.00522,  2.03083,  0.63527,  1.03771, 1.27881, 4.10186,  5.34396],
        [0.586514, 3.371623, 0.860791, 2.92517,  1.14616, 2.99776, 2.26505,  5.62137],
        [1.69704,  1.13912,  2.3595,   4.037552, 1.63698, 1.27549, 0.328671, 0.302282],
        [5.21662,  6.04363,  0.224184, 1.53913,  1.64524, 4.79508, 1.49742,  1.545]
     ]     
    trainingVectors=data['TrainingData']['Features'],      
    trainingLabels=data['TrainingData']['Labels'],
    initialParameters=parameter_starting_points
    qubit_result = TrainHalfMoonModel.estimate_resources(trainingVectors=trainingVectors,trainingLabels=trainingLabels,initialParameters=initialParameters)
    
    #qubit_result = TrainHalfMoonModel.estimate_resources()
    print(qubit_result)

The error information shows like this. It seems that the parameters can't be parsed correctly.

Received invalid parameters. Please fix and try again:
 trainingVectors: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[Microsoft.Quantum.Simulation.Core.IQArray`1[System.Double]]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path '@type', line 1, position 9.
 trainingLabels: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[System.Int64]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path '@type', line 1, position 9.
Traceback (most recent call last):
  File "D:\vscode_python\HelloQ\learnQ\machineLearning\evaluate.py", line 19, in <module>
    qubit_result = TrainHalfMoonModel.estimate_resources(trainingVectors=trainingVectors,trainingLabels=trainingLabels,initialParameters=initialParameters)
  File "D:\software\python\Anaconda3\envs\qsharp-env\lib\site-packages\qsharp\loader.py", line 101, in estimate_resources
    return qsharp.client.estimate(self, **kwargs)
  File "D:\software\python\Anaconda3\envs\qsharp-env\lib\site-packages\qsharp\clients\iqsharp.py", line 191, in estimate
    for row in raw_counts:
TypeError: 'NoneType' object is not iterable

qubit_result = TrainHalfMoonModel.estimate_resources({'trainingVectors':trainingVectors,'trainingLabels':trainingLabels,'initialParameters':initialParameters})

or

qubit_result = TrainHalfMoonModel.estimate_resources([trainingVectors,trainingLabels,initialParameters])

They all can't work. So what's the properly format of argument of estimate_resources method when the operation that needed to be evaluated have arguments?

$\endgroup$
1
  • $\begingroup$ Sorry, The first picture is not right to post here. Just ignore it. $\endgroup$
    – W.xueshan
    Commented Mar 29, 2022 at 3:42

1 Answer 1

1
$\begingroup$

Your code has extra commas after assignments of variables trainingVectors and trainingLabels. This turns them into tuples rather than lists that you want to pass as parameters to estimate_resources. Once you remove the commas, you'll get lists (you can check it by printing the types of the variables you got, print(type(trainingVectors))).

This example is not a great one to use with resources estimation, though. Under the hood it performs training by preparing circuits with current parameters, checking the classification results, and tuning the parameters accordingly. Since resources estimator doesn't perform actual simulation, your measurement results will always be 0s, so training will never complete. You'd need to dig into the code and modify it not to rely on measurement results, for example, to quit after one iteration of training yielding the resource counts. Then you can combine that with simulation to get the number of training iterations required to train the model to get the final resource counts.

$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.