├── License.txt ├── README.md ├── SECURITY.md ├── examples ├── machine-learning │ ├── classifiers │ │ ├── creditscores │ │ │ ├── README.md │ │ │ ├── confusionTestClassical.png │ │ │ ├── confusionTestHardwareAlgiers.png │ │ │ ├── confusionTestHardwareAria1.png │ │ │ ├── confusionTestQuantum.png │ │ │ ├── confusionUnlabeledTestBoth.png │ │ │ ├── credit_classifier.m │ │ │ ├── ibm_algiers_pred_1000.mat │ │ │ ├── processCreditData.m │ │ │ ├── quantumCircuitLayer.m │ │ │ ├── quantumCircuitLayerIBM.m │ │ │ ├── run_on_ibm.m │ │ │ └── trainedNetwork.mat │ │ ├── iris │ │ │ ├── README.md │ │ │ ├── confusionTestClassical.png │ │ │ ├── confusionTestIBM.png │ │ │ ├── confusionTestIonQ.png │ │ │ ├── confusionTestQuantum.png │ │ │ ├── iris.m │ │ │ ├── quantumCircuitLayer.m │ │ │ └── trainedIrisModel.mat │ │ ├── mnist │ │ │ ├── PQCLayer.m │ │ │ ├── README.md │ │ │ ├── mnist-training-results.png │ │ │ └── mnist.m │ │ └── xor │ │ │ ├── PQCLayer.m │ │ │ └── quantum_network_classification.mlx │ └── vqe │ │ ├── README.md │ │ └── VQE_example.mlx ├── noise │ ├── README.md │ └── qrng.mlx └── portfolio-optimization │ ├── README.md │ └── portfolio_optimization.mlx └── introduction └── basic_qc_operations.mlx /License.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2023, The MathWorks, Inc. 2 | All rights reserved. 3 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 4 | 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 5 | 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 6 | 3. In all cases, the software is, and all modifications and derivatives of the software shall be, licensed to you solely for use in conjunction with MathWorks products and service offerings. 7 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Quantum Computing with MATLAB 2 | 3 | This repository serves as the home page for community members interested in quantum computing 4 | with MATLAB®. The goal is to provide examples, functions and otherwise helpful material using the [MATLAB 5 | Support Package for Quantum Computing](https://www.mathworks.com/help/matlab/quantum-computing.html), available through the Add-On Explorer or File Exchange. 6 | 7 | ## Required Products 8 | - MATLAB® Support Package for Quantum Computing (version 23.2.0) 9 | - Connection to remote quantum devices and simulators requires additional setup. For more information go to 10 | - [Run Quantum Circuit on Hardware Using AWS](https://www.mathworks.com/help/matlab/math/run-quantum-circuit-on-hardware.html) 11 | - [Run Quantum Circuit on Hardware Using IBM](https://www.mathworks.com/help/matlab/math/run-quantum-circuit-on-hardware-using-IBM.html) 12 | - Examples may depend on other products which are listed in each respective README file. 13 | 14 | 15 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Reporting Security Vulnerabilities 2 | 3 | If you believe you have discovered a security vulnerability, please report it to 4 | [security@mathworks.com](mailto:security@mathworks.com). Please see 5 | [MathWorks Vulnerability Disclosure Policy for Security Researchers](https://www.mathworks.com/company/aboutus/policies_statements/vulnerability-disclosure-policy.html) 6 | for additional information. 7 | -------------------------------------------------------------------------------- /examples/machine-learning/classifiers/creditscores/README.md: -------------------------------------------------------------------------------- 1 | ## Classifying Credit Ratings with Classical and Quantum Models 2 | 3 | This example compares classical and quantum models for a binary classification 4 | task on synthetic credit rating data. Using labeled data from [2], both models are 5 | trained and evaluated on unseen test data. New samples of unlabeled data 6 | are used to compare the simulated predictions of both models. 7 | 8 | The quantum model is based on a Tree Tensor Network (TTN) circuit structure 9 | [1] with post-processing. It was trained classically, and tested on real 10 | quantum hardware as well as simulation. The classical model is a conventional 11 | bagged decision tree. 12 | 13 | Details of data pre-processing are shown in the ```processCreditData.m``` 14 | function. The raw data is available with the Statistics and Machine 15 | Learning Toolbox. It can be accessed by opening the example [2]: 16 | ```matlab 17 | openExample('stats/creditratingdemo') 18 | ``` 19 | 20 | ## Simulation and Hardware Test Results 21 | 22 | Models were trained using 99% of the data, and tested on the remaining 1%. 23 | Diagonal elements represent the correct test prediction. The simulated 24 | quantum model performs slighly better on test data than the classical 25 | decision tree model. 26 | 27 | ![](confusionTestClassical.png?raw=true) 28 | 29 | ![](confusionTestQuantum.png?raw=true) 30 | 31 | The IBM Algiers quantum device was used with 1000 shots to classify the 32 | test set. 33 | 34 | ![](confusionTestHardwareAlgiers.png?raw=true) 35 | 36 | The Aria 1 quantum device was used with 100 shots to classify 10 samples 37 | from the test set. 38 | 39 | ![](confusionTestHardwareAria1.png?raw=true) 40 | 41 | ## Simulation Test Results on New Data 42 | 43 | Using new and unlabeled data, both models were simulated to test the 44 | similarity of their predictions. Here, the diagonal elements represent the 45 | two models making the same prediction on unknown samples. The models agree 46 | on many predictions, but the true accuracy is unknown. 47 | 48 | ![](confusionUnlabeledTestBoth.png?raw=true) 49 | 50 | ## Required Products 51 | - MATLAB® Support Package for Quantum Computing 52 | - Statistics and Machine Learning Toolbox 53 | - Deep Learning Toolbox 54 | 55 | ## References 56 | [1] Hierarchical quantum classifiers (Grant et al. 2018) 57 | [2] https://www.mathworks.com/help/stats/credit-rating-by-bagging-decision-trees.html -------------------------------------------------------------------------------- /examples/machine-learning/classifiers/creditscores/confusionTestClassical.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Quantum-Computing-MATLAB/57da66120b5e96c4b0e689ebf5fc62c78f7d142e/examples/machine-learning/classifiers/creditscores/confusionTestClassical.png -------------------------------------------------------------------------------- /examples/machine-learning/classifiers/creditscores/confusionTestHardwareAlgiers.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Quantum-Computing-MATLAB/57da66120b5e96c4b0e689ebf5fc62c78f7d142e/examples/machine-learning/classifiers/creditscores/confusionTestHardwareAlgiers.png -------------------------------------------------------------------------------- /examples/machine-learning/classifiers/creditscores/confusionTestHardwareAria1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Quantum-Computing-MATLAB/57da66120b5e96c4b0e689ebf5fc62c78f7d142e/examples/machine-learning/classifiers/creditscores/confusionTestHardwareAria1.png -------------------------------------------------------------------------------- /examples/machine-learning/classifiers/creditscores/confusionTestQuantum.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Quantum-Computing-MATLAB/57da66120b5e96c4b0e689ebf5fc62c78f7d142e/examples/machine-learning/classifiers/creditscores/confusionTestQuantum.png -------------------------------------------------------------------------------- /examples/machine-learning/classifiers/creditscores/confusionUnlabeledTestBoth.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Quantum-Computing-MATLAB/57da66120b5e96c4b0e689ebf5fc62c78f7d142e/examples/machine-learning/classifiers/creditscores/confusionUnlabeledTestBoth.png -------------------------------------------------------------------------------- /examples/machine-learning/classifiers/creditscores/credit_classifier.m: -------------------------------------------------------------------------------- 1 | 2 | % Load and process the data into labeled samples with 4 features 3 | labeledData = readtable("CreditRating_Historical.dat"); 4 | [X, Y] = processCreditData(labeledData); 5 | 6 | % numeric features (1,4) -> categorical label {better, worse} 7 | 8 | numSamples = size(X,1); 9 | numFeatures = size(X,2); 10 | numClasses = 2; 11 | 12 | rng default 13 | 14 | % Use 99% of the data to train the models 15 | partition = cvpartition(numSamples, "HoldOut", 0.01); 16 | idx = partition.training; 17 | 18 | trainX = X(idx,:); 19 | trainY = Y(idx); 20 | testX = X(~idx,:); 21 | testY = Y(~idx); 22 | 23 | % Save the test data so it can be loaded when running on hardware 24 | save('testDataCredit.mat', 'testX', 'testY') 25 | 26 | %% Evaluate Classical Model 27 | 28 | % Ensemble of bagged decision trees 29 | numTrees = 25; 30 | dTree = TreeBagger(numTrees,trainX,trainY); 31 | 32 | predY = predict(dTree, testX); 33 | 34 | figure 35 | accur = sum(testY==categorical(predY))/numel(testY); 36 | confusionchart(testY, categorical(predY)) 37 | title("Classical Test Results") 38 | 39 | %% Evaluate Quantum Model 40 | 41 | % Quantum TTN circuit (Figure 6 [1]) with classical processing 42 | % on the expectation value of a qubit 43 | % layers = [ 44 | % featureInputLayer(numFeatures) 45 | % quantumCircuitLayer 46 | % fullyConnectedLayer(numClasses) 47 | % softmaxLayer 48 | % classificationLayer]; 49 | % 50 | % options = trainingOptions("adam", ... 51 | % MiniBatchSize=50, ... 52 | % InitialLearnRate=0.01, ... 53 | % ExecutionEnvironment="cpu", ... 54 | % Verbose=false, ... 55 | % MaxEpochs=25, ... 56 | % Plots="training-progress"); 57 | % 58 | % net = trainNetwork(trainX,trainY,layers,options); 59 | % save('trainedNetwork.mat', 'net') 60 | 61 | trained = load('trainedNetwork.mat'); 62 | net = trained.net; 63 | 64 | predY = classify(net, testX); 65 | 66 | figure 67 | accur = sum(testY==predY)/numel(testY); 68 | confusionchart(testY, predY); 69 | title("Simulation Test Results") 70 | 71 | %% Evaluate Models on New Data 72 | 73 | % The quantum and classical models were tested using unseen labeled data. 74 | % Now compare the predictions of both models on new unlabeled data. 75 | 76 | % unlabeledData = readtable("CreditRating_NewCompanies.dat"); 77 | % X = processCreditData(unlabeledData); 78 | % 79 | % Y1 = predict(dTree, X); 80 | % Y1 = categorical(Y1); 81 | % 82 | % Y2 = classify(net, X); 83 | % 84 | % figure 85 | % accur = sum(Y1==Y2)/size(X,1); 86 | % cm = confusionchart(Y1, Y2); 87 | % cm.XLabel = "Classical"; 88 | % cm.YLabel = "Quantum"; -------------------------------------------------------------------------------- /examples/machine-learning/classifiers/creditscores/ibm_algiers_pred_1000.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Quantum-Computing-MATLAB/57da66120b5e96c4b0e689ebf5fc62c78f7d142e/examples/machine-learning/classifiers/creditscores/ibm_algiers_pred_1000.mat -------------------------------------------------------------------------------- /examples/machine-learning/classifiers/creditscores/processCreditData.m: -------------------------------------------------------------------------------- 1 | function [X,Y] = processCreditData(data) 2 | % Ratings {A,AA,AAA,B} are labeled as better and {BB,BBB,C} are labeled 3 | % as worse. Use 4 features to classify a sample as worse or better: 4 | 5 | % (RE_TA) - Retained Earnings / Total Assets 6 | % (EBIT_TA) - Earnings Before Interests and Taxes / Total Assets 7 | % (MVE_BVTD) - Market Value of Equity / Book Value of Total Debt 8 | % (S_TA) - Sales / Total Assets 9 | 10 | arguments 11 | data table 12 | end 13 | 14 | vars = data.Properties.VariableNames; 15 | 16 | % Choose features and rescale them in the range [0 pi/2] 17 | features = vars(3:6); 18 | data = normalize(data,"range", [0 pi/2], "DataVariables",features); 19 | 20 | numSamples = size(data, 1); 21 | numFeatures = length(features); 22 | 23 | hasLabels = ismember('Rating', vars); 24 | if hasLabels 25 | Y = categorical(repmat("worse", [numSamples 1])); 26 | else 27 | % Unused default output when data doesn't have labels 28 | Y = missing; 29 | end 30 | 31 | X = zeros(numSamples, numFeatures); 32 | for ii = 1:numSamples 33 | X(ii,:) = data{ii, features}; 34 | if hasLabels 35 | if ismember(data.Rating(ii), {'A','AA','AAA','B'}) 36 | Y(ii) = categorical("better"); 37 | end 38 | end 39 | end 40 | end -------------------------------------------------------------------------------- /examples/machine-learning/classifiers/creditscores/quantumCircuitLayer.m: -------------------------------------------------------------------------------- 1 | classdef quantumCircuitLayer < nnet.layer.Layer 2 | properties (Learnable) 3 | Weights 4 | end 5 | 6 | methods 7 | function layer = quantumCircuitLayer 8 | 9 | layer.Weights = rand(7,1); 10 | end 11 | 12 | function Z = predict(layer, X) 13 | % Z = predict(layer, X) forwards the input data X through the 14 | % layer and outputs the result Z. This method is used during 15 | % prediction. 16 | 17 | Z = expval(X, layer.Weights); 18 | end 19 | 20 | function [Z,memory] = forward(layer, X) 21 | % Z = forward(layer, X) forwards the input data X through the 22 | % layer and outputs the result Z. This method is used during 23 | % training. 24 | 25 | Z = expval(X, layer.Weights); 26 | memory = []; 27 | end 28 | 29 | function [dLdX,dLdW] = backward(layer,X,~,dLdZ,~) 30 | % Backward propagate the derivative of the loss 31 | % function through the layer. 32 | % 33 | % Inputs: 34 | % layer - Layer to backward propagate through 35 | % X - Layer input data 36 | % Z - Layer output data 37 | % dLdZ - Derivative of loss with respect to layer 38 | % output 39 | % memory - Memory value from forward function 40 | % Outputs: 41 | % dLdX - Derivative of loss with respect to layer input 42 | % dLdW - Derivative of loss with respect to 43 | % learnable parameters 44 | 45 | s = pi/4; 46 | dLdW = zeros(size(layer.Weights),'like',layer.Weights); 47 | 48 | for i=1:size(layer.Weights, 1) 49 | % Parameter-Shift 50 | WPlus = layer.Weights; 51 | WPlus(i) = WPlus(i) + s; 52 | ZPlus = expval(X, WPlus); 53 | 54 | WMinus = layer.Weights; 55 | WMinus(i) = WMinus(i) - s; 56 | ZMinus = expval(X,WMinus); 57 | 58 | dZdWi = (ZPlus - ZMinus)/(2*sin(s)); 59 | dLdW(i) = sum(dLdZ .* dZdWi, 'all'); 60 | end 61 | 62 | % Make this all zero since this gradient is not going to be 63 | % used during training. 64 | dLdX = zeros(size(X), 'like', X); 65 | end 66 | end 67 | end 68 | 69 | function Z = expval(X, weights) 70 | 71 | numSamples = size(X,2); 72 | numQubits = size(X,1); % Each qubit encodes a pixel 73 | Z = zeros(1,numSamples,'like',X); 74 | readout = 3; 75 | 76 | % TTN Classifier, see Figure 6 [1] 77 | paramGates = [ryGate(1:numQubits, weights(1:numQubits)) 78 | cxGate([1 4], [2 3]) 79 | ryGate([2 3], weights(numQubits+1:end-1)) 80 | cxGate(2,3) 81 | ryGate(readout, weights(end)) 82 | ]; 83 | 84 | for i = 1:numSamples 85 | % Encode features 86 | encodingGates = [ryGate(1:numQubits, 2*X(:,i))]; 87 | mdl = quantumCircuit([encodingGates; paramGates]); 88 | 89 | % Compute expectation value of Pauli Z operator on the measured qubit 90 | result = simulate(mdl); 91 | Z(i) = probability(result,readout,"0") - probability(result,readout,"1"); 92 | end 93 | end -------------------------------------------------------------------------------- /examples/machine-learning/classifiers/creditscores/quantumCircuitLayerIBM.m: -------------------------------------------------------------------------------- 1 | classdef quantumCircuitLayerIBM < nnet.layer.Layer 2 | properties (Learnable) 3 | Weights 4 | end 5 | 6 | properties 7 | Backend 8 | end 9 | 10 | methods 11 | function layer = quantumCircuitLayerIBM(backend, weights) 12 | arguments 13 | backend 14 | weights 15 | end 16 | 17 | assert(isa(backend, "quantum.backend.QuantumDevice")) 18 | 19 | layer.Backend = backend; 20 | layer.Weights = weights; 21 | end 22 | 23 | function Z = predict(layer, X) 24 | % Z = predict(layer, X) forwards the input data X through the 25 | % layer and outputs the result Z. This method is used during 26 | % prediction. 27 | 28 | Z = expval(layer, X, layer.Weights); 29 | end 30 | 31 | function [Z,memory] = forward(layer, X) 32 | % Z = forward(layer, X) forwards the input data X through the 33 | % layer and outputs the result Z. This method is used during 34 | % training. 35 | 36 | Z = expval(layer, X, layer.Weights); 37 | memory = []; 38 | end 39 | 40 | function [dLdX,dLdW] = backward(layer,X,~,dLdZ,~) 41 | % Backward propagate the derivative of the loss 42 | % function through the layer. 43 | % 44 | % Inputs: 45 | % layer - Layer to backward propagate through 46 | % X - Layer input data 47 | % Z - Layer output data 48 | % dLdZ - Derivative of loss with respect to layer 49 | % output 50 | % memory - Memory value from forward function 51 | % Outputs: 52 | % dLdX - Derivative of loss with respect to layer input 53 | % dLdW - Derivative of loss with respect to 54 | % learnable parameters 55 | 56 | s = pi/4; 57 | dLdW = zeros(size(layer.Weights),'like',layer.Weights); 58 | 59 | for i=1:size(layer.Weights, 1) 60 | % Parameter-Shift 61 | WPlus = layer.Weights; 62 | WPlus(i) = WPlus(i) + s; 63 | ZPlus = expval(layer, X, WPlus); 64 | 65 | WMinus = layer.Weights; 66 | WMinus(i) = WMinus(i) - s; 67 | ZMinus = expval(layer, X,WMinus); 68 | 69 | dZdWi = (ZPlus - ZMinus)/(2*sin(s)); 70 | dLdW(i) = sum(dLdZ .* dZdWi, 'all'); 71 | end 72 | 73 | % Make this all zero since this gradient is not going to be 74 | % used during training. 75 | dLdX = zeros(size(X), 'like', X); 76 | end 77 | 78 | function Z = expval(layer, X, weights) 79 | 80 | if all(X == [1;1;1;1]) 81 | % Don't run the circuit during the validation step of the 82 | % network 83 | Z = zeros(1, 'like', X); 84 | return 85 | end 86 | 87 | numSamples = size(X,2); 88 | numQubits = size(X,1); 89 | Z = zeros(1,numSamples,'like',X); 90 | readout = 3; 91 | 92 | % TTN Classifier, see Figure 6 [1] 93 | paramGates = [ryGate(1:numQubits, weights(1:numQubits)) 94 | cxGate([1 4], [2 3]) 95 | ryGate([2 3], weights(numQubits+1:end-1)) 96 | cxGate(2,3) 97 | ryGate(readout, weights(end)) 98 | ]; 99 | 100 | % Submit a task for each data sample 101 | tasks = cell(numSamples, 1); 102 | for i = 1:numSamples 103 | encodingGates = [ryGate(1:numQubits, 2*X(:,i))]; 104 | mdl = quantumCircuit([encodingGates; paramGates]); 105 | 106 | task = run(mdl, layer.Backend, NumShots=1000); 107 | tasks{i} = task; 108 | end 109 | 110 | for i = 1:numSamples 111 | t = tasks{i}; 112 | wait(t) 113 | result = fetchOutput(t); 114 | % Compute expectation value of Pauli Z operator on the measured qubit 115 | Z(i) = probability(result,readout,"0") - probability(result,readout,"1"); 116 | end 117 | end 118 | end 119 | end -------------------------------------------------------------------------------- /examples/machine-learning/classifiers/creditscores/run_on_ibm.m: -------------------------------------------------------------------------------- 1 | 2 | % Load the trained network 3 | % trained = load('trainedNetwork.mat'); 4 | % trainedLayers = trained.net.Layers; 5 | 6 | % Account setup required for constructing QuantumDeviceIBM can be found at: 7 | % https://www.mathworks.com/help/matlab/math/run-quantum-circuit-on-hardware-using-IBM.html 8 | 9 | % Construct a new network using the classically trained weights 10 | % device = quantum.backend.QuantumDeviceIBM("ibm_algiers", UseSession=true, AccountName="..."); 11 | % layers = [ 12 | % trainedLayers(1) 13 | % quantumCircuitLayerIBM(device, trainedLayers(2).Weights) 14 | % trainedLayers(3:5) 15 | % ]; 16 | % net = SeriesNetwork(layers); 17 | 18 | % Load and classify test data 19 | testdata = load('testDataCredit.mat'); 20 | % predY = classify(net, testdata.testX); 21 | 22 | results = load('ibm_algiers_pred_1000.mat'); 23 | predY = results.predY; 24 | 25 | figure 26 | accur = sum(testdata.testY==predY)/numel(testdata.testY); 27 | confusionchart(testdata.testY, predY); 28 | title("IBM Algiers Test Results") -------------------------------------------------------------------------------- /examples/machine-learning/classifiers/creditscores/trainedNetwork.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Quantum-Computing-MATLAB/57da66120b5e96c4b0e689ebf5fc62c78f7d142e/examples/machine-learning/classifiers/creditscores/trainedNetwork.mat -------------------------------------------------------------------------------- /examples/machine-learning/classifiers/iris/README.md: -------------------------------------------------------------------------------- 1 | ## Classifying Iris with Classical and Quantum Models 2 | 3 | This example compares classical and quantum models for a binary classification 4 | task on the Iris dataset. Using labeled data from [2], both models are 5 | trained and evaluated on unseen test data. 6 | 7 | The quantum model is based on a Tree Tensor Network (TTN) circuit structure 8 | [1] with post-processing. It was trained classically, and tested on real 9 | quantum hardware as well as simulation. The classical model is a conventional 10 | bagged decision tree. 11 | 12 | The dataset is available with the Statistics and Machine Learning Toolbox. 13 | It can be accessed by opening the example [2]: 14 | ```matlab 15 | openExample('stats/classdemo') 16 | ``` 17 | 18 | ## Simulation and Hardware Test Results 19 | 20 | Models were trained using ~66% of the data, and tested on the remaining ~33%. 21 | Diagonal elements represent the correct prediction. 22 | 23 | ![](confusionTestClassical.png?raw=true) 24 | 25 | ![](confusionTestQuantum.png?raw=true) 26 | 27 | Only 10 samples from the test set were ran on real hardware. The IBM Lagos 28 | quantum device used 500 shots to classify each sample and the IonQ Harmony 29 | device used 100 shots. 30 | 31 | ![](confusionTestIBM.png?raw=true) 32 | 33 | ![](confusionTestIonQ.png?raw=true) 34 | 35 | ## Required Products 36 | - MATLAB® Support Package for Quantum Computing 37 | - Statistics and Machine Learning Toolbox 38 | - Deep Learning Toolbox 39 | 40 | ## References 41 | [1] Hierarchical quantum classifiers (Grant et al. 2018) 42 | [2] https://www.mathworks.com/help/stats/classification-example.html -------------------------------------------------------------------------------- /examples/machine-learning/classifiers/iris/confusionTestClassical.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Quantum-Computing-MATLAB/57da66120b5e96c4b0e689ebf5fc62c78f7d142e/examples/machine-learning/classifiers/iris/confusionTestClassical.png -------------------------------------------------------------------------------- /examples/machine-learning/classifiers/iris/confusionTestIBM.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Quantum-Computing-MATLAB/57da66120b5e96c4b0e689ebf5fc62c78f7d142e/examples/machine-learning/classifiers/iris/confusionTestIBM.png -------------------------------------------------------------------------------- /examples/machine-learning/classifiers/iris/confusionTestIonQ.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Quantum-Computing-MATLAB/57da66120b5e96c4b0e689ebf5fc62c78f7d142e/examples/machine-learning/classifiers/iris/confusionTestIonQ.png -------------------------------------------------------------------------------- /examples/machine-learning/classifiers/iris/confusionTestQuantum.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Quantum-Computing-MATLAB/57da66120b5e96c4b0e689ebf5fc62c78f7d142e/examples/machine-learning/classifiers/iris/confusionTestQuantum.png -------------------------------------------------------------------------------- /examples/machine-learning/classifiers/iris/iris.m: -------------------------------------------------------------------------------- 1 | 2 | % Example from Statistics and Machine Learning Toolbox 3 | data = readtable("fisheriris.csv"); 4 | [X,Y] = processIrisData(data); 5 | 6 | gscatter(X(:,1), X(:,2), Y,'rgb','osd'); 7 | xlabel('Sepal length'); 8 | ylabel('Sepal width'); 9 | 10 | numSamples = size(X,1); 11 | numFeatures = size(X,2); 12 | numClasses = 2; 13 | 14 | rng default 15 | 16 | partition = cvpartition(numSamples, "HoldOut", 0.33); 17 | idx = partition.training; 18 | 19 | trainX = X(idx,:); 20 | trainY = Y(idx); 21 | testX = X(~idx,:); 22 | testY = Y(~idx); 23 | 24 | save('testDataIris.mat', 'testX', "testY") 25 | 26 | %% Classical Model 27 | 28 | % Bagged decision tree 29 | numTrees = 25; 30 | dTree = TreeBagger(numTrees,trainX,trainY); 31 | 32 | predY = predict(dTree, testX); 33 | 34 | figure 35 | accur = sum(testY==predY)/numel(testY); 36 | confusionchart(testY, categorical(predY)); 37 | title('Classical Test Accuracy: '+string(accur)) 38 | 39 | %% Quantum Model 40 | 41 | % Build the quantum network 42 | % layers = [ 43 | % featureInputLayer(numFeatures) 44 | % quantumCircuitLayer 45 | % fullyConnectedLayer(numClasses) 46 | % softmaxLayer 47 | % classificationLayer]; 48 | % 49 | % options = trainingOptions("adam", ... 50 | % MiniBatchSize=20, ... 51 | % InitialLearnRate=0.01, ... 52 | % ExecutionEnvironment="cpu", ... 53 | % Verbose=false, ... 54 | % MaxEpochs=10, ... 55 | % Plots="training-progress"); 56 | % 57 | % net = trainNetwork(trainX,trainY,layers,options); 58 | % save('trainedIrisModel.mat', 'net') 59 | 60 | trained = load('trainedIrisModel.mat'); 61 | net = trained.net; 62 | 63 | predY = classify(net, testX); 64 | 65 | figure 66 | accur = sum(testY==predY)/numel(testY); 67 | confusionchart(testY, predY); 68 | title('Quantum Simulation Test Accuracy: '+string(accur)) 69 | 70 | function [X,Y] = processIrisData(data) 71 | % See Table 2 [1] 72 | arguments 73 | data table 74 | end 75 | 76 | % Select data from two of the three classes for binary classification 77 | classes = {'setosa', 'virginica'}; 78 | selection = ismember(data.Species, classes); 79 | data = data(selection, :); 80 | 81 | % Rescale features in the range [0 pi/2] 82 | vars = data.Properties.VariableNames; 83 | features = vars(1:4); 84 | data = normalize(data,"range", [0 pi/2], "DataVariables",features); 85 | 86 | numSamples = size(data, 1); 87 | numFeatures = length(features); 88 | 89 | Y = categorical(repmat(classes(1), [numSamples 1])); 90 | X = zeros(numSamples, numFeatures); 91 | for ii = 1:numSamples 92 | X(ii,:) = data{ii, features}; 93 | if isequal(data.Species{ii}, classes{2}) 94 | Y(ii) = categorical(classes(2)); 95 | end 96 | end 97 | end -------------------------------------------------------------------------------- /examples/machine-learning/classifiers/iris/quantumCircuitLayer.m: -------------------------------------------------------------------------------- 1 | classdef quantumCircuitLayer < nnet.layer.Layer 2 | properties (Learnable) 3 | Weights 4 | end 5 | 6 | methods 7 | function layer = quantumCircuitLayer 8 | 9 | layer.Weights = rand(7,1); 10 | end 11 | 12 | function Z = predict(layer, X) 13 | % Z = predict(layer, X) forwards the input data X through the 14 | % layer and outputs the result Z. This method is used during 15 | % prediction. 16 | 17 | Z = expval(X, layer.Weights); 18 | end 19 | 20 | function [Z,memory] = forward(layer, X) 21 | % Z = forward(layer, X) forwards the input data X through the 22 | % layer and outputs the result Z. This method is used during 23 | % training. 24 | 25 | Z = expval(X, layer.Weights); 26 | memory = []; 27 | end 28 | 29 | function [dLdX,dLdW] = backward(layer,X,~,dLdZ,~) 30 | % Backward propagate the derivative of the loss 31 | % function through the layer. 32 | % 33 | % Inputs: 34 | % layer - Layer to backward propagate through 35 | % X - Layer input data 36 | % Z - Layer output data 37 | % dLdZ - Derivative of loss with respect to layer 38 | % output 39 | % memory - Memory value from forward function 40 | % Outputs: 41 | % dLdX - Derivative of loss with respect to layer input 42 | % dLdW - Derivative of loss with respect to 43 | % learnable parameters 44 | 45 | s = pi/4; 46 | dLdW = zeros(size(layer.Weights),'like',layer.Weights); 47 | 48 | for i=1:size(layer.Weights, 1) 49 | % Parameter-Shift 50 | WPlus = layer.Weights; 51 | WPlus(i) = WPlus(i) + s; 52 | ZPlus = expval(X, WPlus); 53 | 54 | WMinus = layer.Weights; 55 | WMinus(i) = WMinus(i) - s; 56 | ZMinus = expval(X,WMinus); 57 | 58 | dZdWi = (ZPlus - ZMinus)/(2*sin(s)); 59 | dLdW(i) = sum(dLdZ .* dZdWi, 'all'); 60 | end 61 | 62 | % Make this all zero since this gradient is not going to be 63 | % used during training. 64 | dLdX = zeros(size(X), 'like', X); 65 | end 66 | end 67 | end 68 | 69 | function Z = expval(X, weights) 70 | 71 | numSamples = size(X,2); 72 | numQubits = size(X,1); % Each qubit encodes a pixel 73 | Z = zeros(1,numSamples,'like',X); 74 | readout = 3; 75 | 76 | % TTN Classifier, see Figure 6 [1] 77 | paramGates = [ryGate(1:numQubits, weights(1:numQubits)) 78 | cxGate([1 4], [2 3]) 79 | ryGate([2 3], weights(numQubits+1:end-1)) 80 | cxGate(2,3) 81 | ryGate(readout, weights(end)) 82 | ]; 83 | 84 | for i = 1:numSamples 85 | % Encode features 86 | encodingGates = [ryGate(1:numQubits, 2*X(:,i))]; 87 | mdl = quantumCircuit([encodingGates; paramGates]); 88 | 89 | % Compute expectation value of Pauli Z operator on the measured qubit 90 | sv = simulate(mdl); 91 | result = randsample(sv, 100); 92 | Z(i) = probability(result,readout,"0") - probability(result,readout,"1"); 93 | end 94 | end -------------------------------------------------------------------------------- /examples/machine-learning/classifiers/iris/trainedIrisModel.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Quantum-Computing-MATLAB/57da66120b5e96c4b0e689ebf5fc62c78f7d142e/examples/machine-learning/classifiers/iris/trainedIrisModel.mat -------------------------------------------------------------------------------- /examples/machine-learning/classifiers/mnist/PQCLayer.m: -------------------------------------------------------------------------------- 1 | classdef PQCLayer < nnet.layer.Layer 2 | properties (Learnable) 3 | Weights 4 | end 5 | 6 | properties 7 | Backend 8 | numLearnables = 14; 9 | end 10 | 11 | methods 12 | function layer = PQCLayer(backend, weights) 13 | arguments 14 | backend = "local" 15 | weights = rand(14, 1) 16 | end 17 | 18 | assert(isequal(size(weights), [layer.numLearnables 1])) 19 | 20 | layer.Backend = backend; 21 | layer.Weights = weights; 22 | end 23 | 24 | function Z = predict(layer, X) 25 | % Z = predict(layer, X) forwards the input data X through the 26 | % layer and outputs the result Z. This method is used during 27 | % prediction. 28 | 29 | Z = expval(X, layer.Weights, layer.Backend); 30 | end 31 | 32 | function [Z,memory] = forward(layer, X) 33 | % Z = forward(layer, X) forwards the input data X through the 34 | % layer and outputs the result Z. This method is used during 35 | % training. 36 | 37 | Z = expval(X, layer.Weights, layer.Backend); 38 | memory = []; 39 | end 40 | 41 | function [dLdX,dLdW] = backward(layer,X,~,dLdZ,~) 42 | % Backward propagate the derivative of the loss 43 | % function through the layer. 44 | % 45 | % Inputs: 46 | % layer - Layer to backward propagate through 47 | % X - Layer input data 48 | % Z - Layer output data 49 | % dLdZ - Derivative of loss with respect to layer 50 | % output 51 | % memory - Memory value from forward function 52 | % Outputs: 53 | % dLdX - Derivative of loss with respect to layer input 54 | % dLdW - Derivative of loss with respect to 55 | % learnable parameters 56 | 57 | s = pi/4; 58 | dLdW = zeros(size(layer.Weights),'like',layer.Weights); 59 | 60 | for i=1:size(layer.Weights, 1) 61 | % Parameter-Shift 62 | WPlus = layer.Weights; 63 | WPlus(i) = WPlus(i) + s; 64 | ZPlus = expval(X, WPlus, layer.Backend); 65 | 66 | WMinus = layer.Weights; 67 | WMinus(i) = WMinus(i) - s; 68 | ZMinus = expval(X,WMinus, layer.Backend); 69 | 70 | dZdWi = (ZPlus - ZMinus)/(2*sin(s)); 71 | dLdW(i) = sum(dLdZ .* dZdWi, 'all'); 72 | end 73 | 74 | % Make this all zero since this gradient is not going to be 75 | % used during training. 76 | dLdX = zeros(size(X), 'like', X); 77 | end 78 | end 79 | end 80 | 81 | function Z = expval(X, weights, backend) 82 | 83 | numSamples = size(X,2); 84 | numQubits = size(X,1); % Each qubit encodes a pixel 85 | Z = zeros(1,numSamples,'like',X); 86 | readout = 6; 87 | 88 | % TTN Classifier 89 | paramGates = [ryGate(1:numQubits, weights(1:numQubits)) 90 | cxGate(1:2:numQubits-1, 2:2:numQubits) 91 | ryGate([2 3 6 7], weights(numQubits+1:end-2)) 92 | cxGate([2 6], [3 7]) 93 | ryGate([3 6], weights(end-1:end)) 94 | cxGate(3, 6)]; 95 | 96 | for i = 1:numSamples 97 | % Encode features 98 | encodingGates = [ryGate(1:numQubits, 2*X(:,i))]; 99 | mdl = quantumCircuit([encodingGates; paramGates]); 100 | 101 | if isequal(backend, "local") 102 | % Simulate model using local resources 103 | result = simulate(mdl); 104 | else 105 | assert(isa(backend, "quantum.backend.QuantumDeviceAWS")) 106 | % Run model using remote resources 107 | task = run(mdl, backend); 108 | wait(task) 109 | result = fetchOutput(task); 110 | end 111 | 112 | % Compute expectation value of Pauli Z operator on the measured qubit 113 | Z(i) = probability(result,readout,"0") - probability(result,readout,"1"); 114 | end 115 | end -------------------------------------------------------------------------------- /examples/machine-learning/classifiers/mnist/README.md: -------------------------------------------------------------------------------- 1 | # Quantum Classifier Network for MNIST Images 2 | 3 | This example simulates the training of a quantum neural network for a binary classification 4 | task on the MNIST dataset. The quantum network is trained locally and evaluated 5 | on a remote device. The data pre-processing and circuit struture is inspired from [1]. 6 | 7 | The same binary classification is compared to a fully-classical neural network [2]. 8 | 9 | The training progress of the quantum network is displayed below and is comparable 10 | to Figure 3 of [1]. 11 | 12 | ![](mnist-training-results.png?raw=true) 13 | 14 | ## Required Products 15 | - MATLAB® Support Package for Quantum Computing 16 | - Statistics and Machine Learning Toolbox 17 | - Deep Learning Toolbox 18 | 19 | ## References 20 | [1] Hierarchical quantum classifiers (Grant et al. 2018) 21 | [2] https://www.mathworks.com/help/deeplearning/ug/create-simple-deep-learning-network-for-classification.html 22 | -------------------------------------------------------------------------------- /examples/machine-learning/classifiers/mnist/mnist-training-results.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Quantum-Computing-MATLAB/57da66120b5e96c4b0e689ebf5fc62c78f7d142e/examples/machine-learning/classifiers/mnist/mnist-training-results.png -------------------------------------------------------------------------------- /examples/machine-learning/classifiers/mnist/mnist.m: -------------------------------------------------------------------------------- 1 | %% MNIST Data 2 | 3 | % Create a datastore of MNIST images 4 | digitDatasetPath = fullfile(matlabroot,'toolbox','nnet','nndemos', ... 5 | 'nndatasets','DigitDataset'); 6 | imds = imageDatastore(digitDatasetPath, ... 7 | 'IncludeSubfolders',true,'LabelSource','foldernames'); 8 | 9 | % Take data corresponding to digits 0 and 1 10 | classLabels = categorical(["0"; "1"]); 11 | numClasses = length(classLabels); 12 | 13 | % Partition into train and test sets 14 | rng default 15 | [imdsTrain,imdsTest] = splitEachLabel(imds,0.8,'randomize','Include',classLabels); 16 | 17 | % Restrict labels to the desired classes 18 | imdsTrain.Labels = categorical(imdsTrain.Labels, classLabels); 19 | imdsTest.Labels = categorical(imdsTest.Labels, classLabels); 20 | 21 | %% Classical Network 22 | 23 | % Build and train the classical network 24 | layers = [ 25 | imageInputLayer([28 28 1]) 26 | convolution2dLayer(3,8,'Padding','same') 27 | batchNormalizationLayer 28 | reluLayer 29 | maxPooling2dLayer(2,'Stride',2) 30 | convolution2dLayer(3,16,'Padding','same') 31 | batchNormalizationLayer 32 | reluLayer 33 | maxPooling2dLayer(2,'Stride',2) 34 | convolution2dLayer(3,32,'Padding','same') 35 | batchNormalizationLayer 36 | reluLayer 37 | fullyConnectedLayer(numClasses) 38 | softmaxLayer 39 | classificationLayer]; 40 | 41 | options = trainingOptions('adam', ... 42 | InitialLearnRate=0.01, ... 43 | ExecutionEnvironment="gpu", ... 44 | MaxEpochs=4, ... 45 | Verbose=false); 46 | 47 | classicalNet = trainNetwork(imdsTrain,layers,options); 48 | 49 | predY = classify(classicalNet, imdsTest); 50 | confusionchart(imdsTest.Labels, predY); 51 | 52 | %% Quantum Network 53 | 54 | % Process ImageDataStore into features and labels 55 | [trainX,trainY] = processIMDS(imdsTrain); 56 | [testX,testY] = processIMDS(imdsTest); 57 | 58 | % Build the quantum network and train it using local resources 59 | inputSize = size(trainX,2); 60 | layers = [ 61 | featureInputLayer(inputSize) 62 | PQCLayer("local") 63 | fullyConnectedLayer(numClasses) 64 | softmaxLayer 65 | classificationLayer]; 66 | 67 | options = trainingOptions("adam", ... 68 | MiniBatchSize=20, ... 69 | InitialLearnRate=0.1, ... 70 | ExecutionEnvironment="gpu", ... 71 | Verbose=true, ... 72 | Plots="training-progress"); 73 | 74 | quantumNetLocal = trainNetwork(trainX,trainY,layers,options); 75 | 76 | % Test the network using remote resources by constructing a new network with 77 | % the locally trained parameters. The PQCLayer will now execute on the 78 | % remote device. 79 | dev = quantum.backend.QuantumDeviceAWS('sv1'); 80 | 81 | layers = [ 82 | quantumNetLocal.Layers(1) 83 | PQCLayer(dev, quantumNetLocal.Layers(2).Weights) 84 | quantumNetLocal.Layers(3:5) 85 | ]; 86 | 87 | quantumNetRemote = SeriesNetwork(layers); 88 | 89 | % Submit 1 sample to evaluate the model on the remote device 90 | predY = classify(quantumNetRemote, testX(1,:)); 91 | 92 | %% Helper functions 93 | function [X,Y] = processIMDS(imds) 94 | % Convert MNIST images into features data X and labels Y 95 | % Input images (28,28) -> PCA features (1,8) 96 | arguments 97 | imds (1,1) matlab.io.datastore.ImageDatastore 98 | end 99 | 100 | Y = categorical(imds.Labels); 101 | 102 | numSamples = size(imds.Labels,1); 103 | X = zeros(numSamples, 28*28); 104 | for i = 1:numSamples 105 | X(i,:) = reshape(double(readimage(imds, i)) ./ 255, [1 28*28]); 106 | end 107 | 108 | [~, scores, ~, ~, ~] = pca(X, NumComponents=8); 109 | X = rescale(scores, 0, pi/2); 110 | end 111 | -------------------------------------------------------------------------------- /examples/machine-learning/classifiers/xor/PQCLayer.m: -------------------------------------------------------------------------------- 1 | classdef PQCLayer < nnet.layer.Layer 2 | % Custom PQC layer example. 3 | 4 | properties (Learnable) 5 | % Define layer learnable parameters. 6 | A 7 | B 8 | end 9 | 10 | methods 11 | function layer = PQCLayer 12 | % Set layer name. 13 | layer.Name = "PQC"; 14 | 15 | % Set layer description. 16 | layer.Description = "Layer containing a parameterized " + ... 17 | "quantum circuit (PQC)"; 18 | 19 | % Initialize learnable parameter. 20 | layer.A = 1; 21 | layer.B = 2; 22 | end 23 | 24 | function Z = predict(layer,X) 25 | % Z = predict(layer,X) forwards the input data X through the 26 | % layer and outputs the result Z at prediction time. 27 | Z = computeZ(X,layer.A,layer.B); 28 | end 29 | 30 | function [dLdX,dLdA,dLdB] = backward(layer,X,Z,dLdZ,memory) 31 | % Backpropagate the derivative of the loss 32 | % function through the layer. 33 | % 34 | % Inputs: 35 | % layer - Layer though which data backpropagates 36 | % X - Layer input data 37 | % Z - Layer output data 38 | % dLdZ - Derivative of loss with respect to layer 39 | % output 40 | % memory - Memory value from forward function 41 | % Outputs: 42 | % dLdX - Derivative of loss with respect to layer input 43 | % dLdA - Derivative of loss with respect to learnable 44 | % parameter A 45 | % dLdB - Derivative of loss with respect to learnable 46 | % parameter B 47 | 48 | s = pi/4; 49 | ZPlus = computeZ(X,layer.A + s,layer.B); 50 | ZMinus = computeZ(X,layer.A - s,layer.B); 51 | dZdA = X(1,:).*((ZPlus - ZMinus)./(2*sin(X(1,:).*s))); 52 | dLdA = sum(dLdZ.*dZdA,"all"); 53 | 54 | ZPlus = computeZ(X,layer.A,layer.B + s); 55 | ZMinus = computeZ(X,layer.A,layer.B - s); 56 | dZdB = X(2,:).*(((ZPlus - ZMinus)./(2*sin(X(2,:).*s)))); 57 | dLdB = sum(dLdZ.*dZdB,"all"); 58 | 59 | % Set the gradients with respect to x and y to zero 60 | % because the QNN does not use them during training. 61 | dLdX = zeros(size(X),"like",X); 62 | end 63 | end 64 | end 65 | 66 | function Z = computeZ(X, A, B) 67 | numSamples = size(X,2); 68 | x1 = X(1,:); 69 | x2 = X(2,:); 70 | Z = zeros(1,numSamples,"like",X); 71 | for i = 1:numSamples 72 | circ = quantumCircuit(2); 73 | circ.Gates = [rxGate(1,x1(i)*A); rxGate(2,x2(i)*B); cxGate(1,2)]; 74 | s = simulate(circ); 75 | Z(i) = probability(s,2,"0") - probability(s,2,"1"); 76 | end 77 | end -------------------------------------------------------------------------------- /examples/machine-learning/classifiers/xor/quantum_network_classification.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Quantum-Computing-MATLAB/57da66120b5e96c4b0e689ebf5fc62c78f7d142e/examples/machine-learning/classifiers/xor/quantum_network_classification.mlx -------------------------------------------------------------------------------- /examples/machine-learning/vqe/README.md: -------------------------------------------------------------------------------- 1 | ## Variational Quantum Eigensolver 2 | 3 | This example demonstrates finding the minimum energy of an observable Hamiltonian using the variational quantum eigensolver. 4 | 5 | ## Required Products 6 | - MATLAB® Support Package for Quantum Computing 7 | - Global and Optimization Toolbox 8 | - [Quantum Expectation Values on File Exchange](https://www.mathworks.com/matlabcentral/fileexchange/165101-quantum-expectation-values) -------------------------------------------------------------------------------- /examples/machine-learning/vqe/VQE_example.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Quantum-Computing-MATLAB/57da66120b5e96c4b0e689ebf5fc62c78f7d142e/examples/machine-learning/vqe/VQE_example.mlx -------------------------------------------------------------------------------- /examples/noise/README.md: -------------------------------------------------------------------------------- 1 | ## Random Number Generation Using Two Independent Quantum Processing Units 2 | 3 | This example shows how to generate a random bit-string using two independent 4 | quantum processing units (QPUs). 5 | 6 | ## Required Products 7 | - MATLAB® Support Package for Quantum Computing 8 | - Optimization Toolbox 9 | 10 | ## References 11 | [1] Mario Berta and Fernando Brandão. "Robust randomness generation on quantum computers." 12 | Preprint (2021). https://marioberta.info/wp-content/uploads/2021/07/randomness-theory.pdf 13 | [2] "Robust randomness generation on quantum processing units." 14 | https://github.com/amazon-braket/amazon-braket-examples/blob/main/examples/advanced_circuits_algorithms/Randomness/Randomness_Generation.ipynb 15 | [3] D. Frauchiger, R. Renner, and M. Troyer. "True randomness from realistic 16 | quantum devices". Preprint (2013) https://doi.org/10.48550/arXiv.1311.4547 17 | [4] R. Konig, R. Renner, and C. Schaffner. "The Operational Meaning of Min- 18 | and Max-Entropy". IEEE Transactions on Information Theory 55(9): 4337-4347 (2009). -------------------------------------------------------------------------------- /examples/noise/qrng.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Quantum-Computing-MATLAB/57da66120b5e96c4b0e689ebf5fc62c78f7d142e/examples/noise/qrng.mlx -------------------------------------------------------------------------------- /examples/portfolio-optimization/README.md: -------------------------------------------------------------------------------- 1 | # Portfolio Optimization using VQE-CVaR 2 | 3 | This example demonstrates a simulation of the Variational Quantum Eigensolver routine using the Conditional Value at Risk 4 | aggregation function [1]. A simple mean-variance portfolio optimization problem [2] is considered as an application to 5 | computational finance. For comparison, the objective is reformulated as a Quadratic Unconstrained Binary Optimization (QUBO) 6 | problem and solved classically. 7 | 8 | The full classical simulation of the VQE optimizes a set of rotation angles used in the variational circuit. The angles from the final 9 | iteration are used in the circuit sent to a real quantum processor. The solution bitstring is measured on the hardware with 10 | the highest probability. 11 | 12 | ## Required Products 13 | - MATLAB® Support Package for Quantum Computing 14 | - Global Optimization Toolbox 15 | 16 | ## References 17 | [1] Improving Variational Quantum Optimization using CVaR (Barkoutsos et al. 2019) 18 | [2] https://www.mathworks.com/help/finance/asset-allocation-case-study.html -------------------------------------------------------------------------------- /examples/portfolio-optimization/portfolio_optimization.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Quantum-Computing-MATLAB/57da66120b5e96c4b0e689ebf5fc62c78f7d142e/examples/portfolio-optimization/portfolio_optimization.mlx -------------------------------------------------------------------------------- /introduction/basic_qc_operations.mlx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mathworks/Quantum-Computing-MATLAB/57da66120b5e96c4b0e689ebf5fc62c78f7d142e/introduction/basic_qc_operations.mlx --------------------------------------------------------------------------------