Esempio n. 1
0
 /// <summary>
 /// Execute the given <code>qasmExe.code</code> launching <code>qasmExe.shots</code> executions.
 /// When the result is ready, the <code>onExecuted</code> callback will be called.
 /// The <see cref="OnExecuted"/> will recieve <see cref="QASMExecutionResult"/>
 /// with the accumulated result.
 /// </summary>
 /// <param name="qasmExe">Executable configuration</param>
 /// <param name="onExecuted">The callback called when execution ends</param>
 public void ExecuteCode(QASMExecutable qasmExe, OnExecuted onExecuted)
 {
     // Request is not needed yet, see "ExecuteCodeRawResult" implementation in case of future changes
     GenericExecution(qasmExe, useMemory: false, (jsonResult) => {
         onExecuted(ReadCountJSON(jsonResult));
     });
 }
Esempio n. 2
0
    /// <summary>
    /// Generates a true random boolean.
    /// It makes an asynchronous operation so the value is returned through
    /// the callback <see cref="OnRandomBoolGenerated"/>
    /// </summary>
    /// <param name="onRandomBoolGenerated">The callback called when the bool is available</param>
    public void GenerateBool(OnRandomBoolGenerated onRandomBoolGenerated)
    {
        // For bool values should be an even number of shots
        QASMExecutable qasmExe = new QASMExecutable(_qasmSingleBoolCode, 15);

        executionSession.ExecuteCode(qasmExe, (response) => {
            onRandomBoolGenerated(response.maxKey == 1);
        });
    }
Esempio n. 3
0
    /// <summary>
    /// Generates a pool of <paramref name="count"/> true random booleans.
    /// It makes an asynchronous operation so the value is returned through
    /// the callback <see cref="OnRandomBoolPoolGenerated"/>.
    /// </summary>
    /// <param name="count">The amount of booleans generated</param>
    /// <param name="onRandomBoolPoolGenerated">The callback called when the pool is available</param>
    public void GenerateBoolPool(int count, OnRandomBoolPoolGenerated onRandomBoolPoolGenerated)
    {
        QASMExecutable qasmExe = new QASMExecutable(_qasmSingleBoolCode, count);

        executionSession.ExecuteCodeRawResult(qasmExe, (response) => {
            List <bool> pool = new List <bool>();
            for (int i = 0; i < response.rawResult.Count; i++)
            {
                pool.Add(response.rawResult[i] == 1);
            }
            onRandomBoolPoolGenerated(pool);
        });
    }
Esempio n. 4
0
    private void GenericExecution(QASMExecutable qasmExe, bool useMemory, OnJsonResult onJsonResult)
    {
        // API request
        List <IMultipartFormSection> formData = new List <IMultipartFormSection> {
            // QASM parameter
            new MultipartFormDataSection("qasm", qasmExe.code),
            new MultipartFormDataSection("memory", useMemory ? "True" : "False")
        };

        // Api token parameter
        if (apiTokenString != "")
        {
            formData.Add(new MultipartFormDataSection("api_token", apiTokenString));
        }
        // Number of shots
        if (qasmExe.useShots)
        {
            formData.Add(new MultipartFormDataSection("shots", $"{qasmExe.shots}"));
        }

        // Request
        UnityWebRequest www = UnityWebRequest.Post(server + "/api/run/qasm", formData);

        www.SendWebRequest().completed += (_) => {
#if UNITY_EDITOR
            if (verbose)
            {
                Debug.Log("text: " + www.downloadHandler.text);
            }
#endif
            if (www.responseCode == 200)   // Is OK
            {
                onJsonResult(GetResultJSON(www.downloadHandler.text));
            }
            else     // ON ERROR
            {
                string responseCodeMessage = $"Response Code: {www.responseCode}";
                if (www.responseCode == 500)
                {
                    responseCodeMessage += " - Internal server error.";
                    if (!string.IsNullOrEmpty(apiTokenString))
                    {
                        responseCodeMessage += "\nIf you are using simulator, consider not to use apiTokenString.";
                    }
                }

                Debug.LogError(responseCodeMessage);
                throw new System.Exception(responseCodeMessage);
            }
        };
    }
Esempio n. 5
0
 /// <summary>
 /// Execute the given <code>qasmExe.code</code> launching <code>qasmExe.shots</code> executions.
 /// When the result is ready, the <code>onExecuted</code> callback will be called.
 /// The <see cref="OnExecuted"/> will recieve <see cref="QASMExecutionResult"/>
 /// with the per shot results as rawResult.
 /// If the backends does not supports memory feature, the raw result will be simulated with
 /// accumulated results.
 /// </summary>
 /// <param name="qasmExe">Executable configuration</param>
 /// <param name="onExecuted">The callback called when execution ends</param>
 public void ExecuteCodeRawResult(QASMExecutable qasmExe, OnExecuted onExecuted)
 {
     RequestBackendConfig((_) => {
         GenericExecution(qasmExe, useMemory: _backendConfig.supportsMemory, (jsonResult) => {
             if (_backendConfig.supportsMemory)
             {
                 onExecuted(ReadRawDataJSON(jsonResult));
             }
             else
             {
                 QASMExecutionResult result = ReadCountJSON(jsonResult);
                 result.SimulateRawResult();
                 onExecuted(result);
             }
         });
     });
 }
Esempio n. 6
0
    /// <summary>
    /// Generates a true random int of n <paramref name="bits"/>.
    /// It makes an asynchronous operation so the value is returned through
    /// the callback <see cref="OnRandomIntGenerated"/>
    /// </summary>
    /// <param name="bits">The number of bits used to generate the int</param>
    /// <param name="onRandomFloatGenerated">The callback called when the int is available</param>
    public void GenerateIntNbits(int bits, OnRandomIntGenerated onRandomIntGenerated)
    {
        executionSession.RequestBackendConfig((backendConfig) => {
            int codeRegs           = Mathf.Min(backendConfig.qubitsCount, bits);
            int shotsNeeded        = Mathf.CeilToInt((float)bits / codeRegs);
            QASMExecutable qasmExe = new QASMExecutable(RandomNRegisterCode(codeRegs), shotsNeeded);

            executionSession.ExecuteCodeRawResult(qasmExe, (response) => {
                int rng = 0;
                for (int i = 0; i < response.rawResult.Count; i++)
                {
                    rng += response.rawResult[i] << (i * codeRegs);
                }
                rng = ClampToBits(rng, bits);
                onRandomIntGenerated(rng);
            });
        });
    }
Esempio n. 7
0
    /// <summary>
    /// Generates a pool of <paramref name="count"/> true random ints of n <paramref name="bits"/>.
    /// It makes an asynchronous operation so the value is returned through
    /// the callback <see cref="OnRandomFloatPoolGenerated"/>.
    /// </summary>
    /// <param name="bits">The number of bits used to generate the int</param>
    /// <param name="count">The amount of ints generated</param>
    /// <param name="onRandomIntPoolGenerated">The callback called when the pool is available</param>
    public void GenerateIntNbitsPool(int bits, int count, OnRandomIntPoolGenerated onRandomIntPoolGenerated)
    {
        executionSession.RequestBackendConfig((backendConfig) => {
            int codeRegs           = Mathf.Min(backendConfig.qubitsCount, bits);
            int shotsNeededPerItem = Mathf.CeilToInt((float)bits / codeRegs);
            QASMExecutable qasmExe = new QASMExecutable(RandomNRegisterCode(codeRegs), shotsNeededPerItem * count);

            executionSession.ExecuteCodeRawResult(qasmExe, (response) => {
                List <int> pool = new List <int>();
                for (int i = 0; i < count; i++)
                {
                    int rng     = 0;
                    int padding = i * shotsNeededPerItem;
                    for (int j = 0; j < shotsNeededPerItem; j++)
                    {
                        rng += response.rawResult[j + padding] << (j * codeRegs);
                    }
                    rng = ClampToBits(rng, bits);
                    pool.Add(rng);
                }
                onRandomIntPoolGenerated(pool);
            });
        });
    }