Azure Quantum Workspace from VScode Job was terminated due to a failed assertion

Nikita Chernevskiy 0 Reputation points
2024-01-26T15:53:20.7466667+00:00

I have this code: host.py

import json
import numpy as np
import matplotlib.pyplot as plt
import qsharp
import qsharp.azure
plt.style.use('ggplot')
from qsharp import azure
from azure.quantum import Workspace
from azure.quantum.optimization import Problem, Solver
import time

from Microsoft.Quantum.Samples import TrainHalfMoonModel

# Azure Quantum workspace information
subscription_id = "ID1"
resource_group = "RG"
workspace_name = "THEWORKSPACENAME"
location = "THELOCATION"
workspace = Workspace(subscription_id=subscription_id, resource_group=resource_group, name=workspace_name, location=location)
targets = qsharp.azure.connect(
            resourceId = "THERESOURCEIT",
            location = "THELOCATION")
# Directly set the Azure Quantum target
qsharp.azure.target("microsoft.estimator")

def train_and_classify(file_path):
    with open(file_path) as f:
        data = json.load(f)

    start_time = time.time()

    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]
    ]

    # Submit the Q# operation to Azure Quantum
    trainingVectors=data['TrainingData']['Features']
    trainingLabels=data['TrainingData']['Labels']
    initialParameters=parameter_starting_points
    
    floral = [row[0] for row in trainingVectors]
    bees = [row[1] for row in trainingVectors]
    
    val1 = [row[0] for row in initialParameters]
    val2 = [row[1] for row in initialParameters]
    val3 = [row[3] for row in initialParameters]
    val4 = [row[4] for row in initialParameters]
    # Quantum computer
    try:
        parameters = qsharp.azure.execute(TrainHalfMoonModel, floral=floral, bees=bees, trainingLabels=trainingLabels, _1=val1, _2=val2, _3=val3, _4=val4, shots=1, qubits=8, jobName=f"Train the model with {file_path}", timeout=3600)
    except Exception as e:
        print(f"Got error: {e}")
        latest_job = max(workspace.list_jobs(), key=lambda job: job.details.creation_time)
        while latest_job.details.status == "Executing":
            time.sleep(10)
            latest_job.refresh()
            print(f"Current job status: {latest_job.details.status}")
    # Regular computer
    #(parameters) = TrainHalfMoonModel(
        #floral=floral, bees=bees, trainingLabels=trainingLabels, _1=val1, _2=val2, _3=val3, _4=val4
    #)

    end_time = time.time()
    training_time = (end_time - start_time)
    rows = len(data['TrainingData']['Features'])

    print(f"Training completed for {file_path}. Rows: {rows}. Time taken: {training_time} seconds")

    return rows, training_time

if __name__ == "__main__":
    json_folder = "jsons"
    files = ["data1.json", "data2.json", "data3.json", "data4.json", "data5.json", "data6.json", "data7.json", "data8.json", "data9.json", "data10.json"]

    amount_tasks = []
    training_times = []

    for file in files:
        time.sleep(10)
        file_path = f"{json_folder}/{file}"
        tasks, time_taken = train_and_classify(file_path)
        amount_tasks.append(tasks)
        training_times.append(time_taken)

    # Plotting the chart
    plt.plot(training_times, amount_tasks, marker='o', linestyle='-')
    plt.ylabel('Amount of Rows')
    plt.xlabel('Time (seconds)')
    plt.title('Training Time vs Amount of Rows')
    plt.show()

And Training.qs

namespace Microsoft.Quantum.Samples {

    open Microsoft.Quantum.Convert;
    open Microsoft.Quantum.Intrinsic;
    open Microsoft.Quantum.Canon;
    open Microsoft.Quantum.Arrays;
    open Microsoft.Quantum.MachineLearning;
    open Microsoft.Quantum.Math;

    function WithProductKernel(scale : Double, sample : Double[]) : Double[] {
        return sample + [scale * Fold(TimesD, 1.0, sample)];
    }

    function Preprocessed(samples : Double[][]) : Double[][] {
        let scale = 1.0;

        return Mapped(
            WithProductKernel(scale, _),
            samples
        );
    }

    function DefaultSchedule(samples : Double[][]) : SamplingSchedule {
        return SamplingSchedule([
            0..Length(samples) - 1
        ]);
    }

    function ClassifierStructure() : ControlledRotation[] {
        return [
            ControlledRotation((0, new Int[0]), PauliX, 4),
            ControlledRotation((0, new Int[0]), PauliZ, 5),
            ControlledRotation((1, new Int[0]), PauliX, 6),
            ControlledRotation((1, new Int[0]), PauliZ, 7),
            ControlledRotation((0, [1]), PauliX, 0),
            ControlledRotation((1, [0]), PauliX, 1),
            ControlledRotation((1, new Int[0]), PauliZ, 2),
            ControlledRotation((1, new Int[0]), PauliX, 3)
        ];
    }

    function CombineFunction(a: Double, b: Double): Double[] {
        return [a, b];
    }

    function CombineFunction4(a: Double, b: Double, c: Double, d: Double): Double[] {
        return [a, b, c, d];
    }

    operation TrainHalfMoonModel(
        floral : Double[],
        bees : Double[],
        trainingLabels : Int[],
        _1 : Double[],
        _2 : Double[],
        _3 : Double[],
        _4 : Double[],
    //) : (Double[], Double) {
    ) : (Double[]) {
        let trainingVectorsArray = Zipped(floral, bees);
        let trainingVectors = Mapped(CombineFunction, trainingVectorsArray);

        let initialParametersArray = Zipped4(_1, _2, _3, _4);
        let initialParameters = Mapped(CombineFunction4, initialParametersArray);
        let samples = Mapped(LabeledSample,Zipped(Preprocessed(trainingVectors), trainingLabels));

        let (optimizedModel, nMisses) = TrainSequentialClassifier(Mapped(SequentialModel(ClassifierStructure(), _, 0.0), initialParameters),
            samples,
            DefaultTrainingOptions()
                w/ LearningRate <- 0.1
                w/ MinibatchSize <- 15
                w/ Tolerance <- 0.005
                w/ NMeasurements <- 10000
                w/ MaxEpochs <- 16,
            DefaultSchedule(trainingVectors),
            DefaultSchedule(trainingVectors)
        );

        //return (optimizedModel::Parameters, optimizedModel::Bias);
        return (optimizedModel::Parameters);
    }

I am connecting to Azure Quantum Workspace. As you have spotted, I am training the classification MLmodel HalfMoon with data like this:

{
  "TrainingData": {
    "Features": [
      [
        86091.0,
        58.0
      ],
      [
        98578.0,
        134.0
      ],
      [
        34851.0,
        34.0
      ],
      [
        26760.0,
        490.0
      ],
      [
        36580.0,
        460.0
      ],
      [
        24205.0,
        441.0
      ],
      [
        44634.0,
        302.0
      ],
      [
        40966.0,
        274.0
      ],
      [
        54651.0,
        148.0
      ],
      [
        28153.0,
        263.0
      ],
      [
        12008.0,
        249.0
      ],
      [
        94799.0,
        30.0
      ],
      [
        84764.0,
        205.0
      ],
      [
        69071.0,
        461.0
      ],
      [
        46563.0,
        486.0
      ],
      [
        22630.0,
        56.0
      ],
      [
        38962.0,
        279.0
      ],
      [
        92771.0,
        377.0
      ],
      [
        22934.0,
        306.0
      ],
      [
        48731.0,
        73.0
      ],
      [
        16857.0,
        294.0
      ],
      [
        11091.0,
        32.0
      ],
      [
        72222.0,
        281.0
      ],
      [
        11591.0,
        131.0
      ],
      [
        30883.0,
        207.0
      ],
      [
        43499.0,
        484.0
      ],
      [
        69419.0,
        150.0
      ],
      [
        55893.0,
        145.0
      ],
      [
        89817.0,
        165.0
      ],
      [
        55651.0,
        329.0
      ],
      [
        56986.0,
        117.0
      ],
      [
        95541.0,
        351.0
      ],
      [
        63641.0,
        212.0
      ],
      [
        42210.0,
        74.0
      ],
      [
        69868.0,
        381.0
      ],
      [
        82337.0,
        138.0
      ],
      [
        62307.0,
        363.0
      ],
      [
        91318.0,
        462.0
      ],
      [
        80484.0,
        307.0
      ],
      [
        71914.0,
        105.0
      ],
      [
        25492.0,
        109.0
      ],
      [
        99223.0,
        452.0
      ],
      [
        19260.0,
        496.0
      ],
      [
        63313.0,
        121.0
      ],
      [
        71126.0,
        438.0
      ],
      [
        83397.0,
        127.0
      ],
      [
        38562.0,
        41.0
      ],
      [
        47829.0,
        369.0
      ],
      [
        30291.0,
        289.0
      ],
      [
        68468.0,
        276.0
      ],
      [
        83562.0,
        234.0
      ],
      [
        86920.0,
        347.0
      ],
      [
        57956.0,
        171.0
      ],
      [
        39641.0,
        267.0
      ],
      [
        15517.0,
        354.0
      ],
      [
        18606.0,
        97.0
      ],
      [
        42499.0,
        339.0
      ],
      [
        52468.0,
        134.0
      ],
      [
        55145.0,
        355.0
      ],
      [
        69510.0,
        138.0
      ],
      [
        87156.0,
        201.0
      ],
      [
        39751.0,
        406.0
      ],
      [
        54010.0,
        401.0
      ],
      [
        35248.0,
        438.0
      ],
      [
        28351.0,
        432.0
      ],
      [
        56506.0,
        25.0
      ],
      [
        46459.0,
        255.0
      ],
      [
        14901.0,
        9.0
      ],
      [
        39117.0,
        141.0
      ],
      [
        72828.0,
        479.0
      ],
      [
        5881.0,
        463.0
      ],
      [
        82888.0,
        285.0
      ],
      [
        75860.0,
        482.0
      ],
      [
        91955.0,
        15.0
      ],
      [
        7376.0,
        444.0
      ],
      [
        16578.0,
        135.0
      ],
      [
        71147.0,
        238.0
      ],
      [
        32673.0,
        248.0
      ],
      [
        52579.0,
        355.0
      ],
      [
        12145.0,
        172.0
      ],
      [
        31105.0,
        399.0
      ],
      [
        20850.0,
        229.0
      ],
      [
        5485.0,
        385.0
      ],
      [
        62891.0,
        24.0
      ],
      [
        13703.0,
        382.0
      ],
      [
        22394.0,
        384.0
      ],
      [
        50084.0,
        500.0
      ],
      [
        11557.0,
        155.0
      ],
      [
        70083.0,
        12.0
      ],
      [
        22979.0,
        455.0
      ],
      [
        75102.0,
        453.0
      ],
      [
        10949.0,
        59.0
      ],
      [
        70348.0,
        61.0
      ],
      [
        43891.0,
        78.0
      ],
      [
        75237.0,
        69.0
      ],
      [
        47461.0,
        114.0
      ],
      [
        71399.0,
        212.0
      ],
      [
        93535.0,
        199.0
      ],
      [
        46460.0,
        106.0
      ],
      [
        42635.0,
        48.0
      ]
    ],
    "Labels": [
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      1,
      1,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      1,
      1,
      0,
      0,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      0,
      0,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      1,
      1,
      0,
      0,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      1,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      1,
      1
    ]
  },
  "ValidationData": {
    "Features": [
      [
        35314.0,
        315.0
      ],
      [
        43170.0,
        410.0
      ]
    ],
    "Labels": [
      0,
      0
    ]
  }
}

the problem is that I receive this error - Job was terminated due to a failed assertion User's image

I had to finish the project, that's why I have this code to bypass the errors. I have no Idea why, but I always have 240 seconds on executing the training and I cant change thatUser's image

except Exception as e

But the new error FailedAssertion still occurs. How can I resolve it? Thanks!

Azure Quantum
Azure Quantum
An Azure service that provides quantum computing and optimization solutions.
60 questions
.NET Machine learning
.NET Machine learning
.NET: Microsoft Technologies based on the .NET software framework.Machine learning: A type of artificial intelligence focused on enabling computers to use observed data to evolve new behaviors that have not been explicitly programmed.
150 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Nikita Chernevskiy 0 Reputation points
    2024-01-26T17:24:14.79+00:00

    I suppose we can close this treat. I just found out microsoft released v1 of SDK for Quantum Computing - that means I have to rewrite the whole code

    0 comments No comments