Quantum Machine Learning

Overview

Qiskit Machine Learning

LicenseBuild StatusCoverage Status

The Machine Learning package simply contains sample datasets at present. It has some classification algorithms such as QSVM and VQC (Variational Quantum Classifier), where this data can be used for experiments, and there is also QGAN (Quantum Generative Adversarial Network) algorithm.

Installation

We encourage installing Qiskit Machine Learning via the pip tool (a python package manager).

pip install qiskit-machine-learning

pip will handle all dependencies automatically and you will always install the latest (and well-tested) version.

If you want to work on the very latest work-in-progress versions, either to try features ahead of their official release or if you want to contribute to Machine Learning, then you can install from source. To do this follow the instructions in the documentation.


Optional Installs

  • PyTorch, may be installed either using command pip install 'qiskit-machine-learning[torch]' to install the package or refer to PyTorch getting started. PyTorch being installed will enable the neural networks PyTorchDiscriminator component to be used with the QGAN algorithm.

Creating Your First Machine Learning Programming Experiment in Qiskit

Now that Qiskit Machine Learning is installed, it's time to begin working with the Machine Learning module. Let's try an experiment using VQC (Variational Quantum Classifier) algorithm to train and test samples from a data set to see how accurately the test set can be classified.

        from qiskit import BasicAer
        from qiskit.utils import QuantumInstance, algorithm_globals
        from qiskit.algorithms.optimizers import COBYLA
        from qiskit.circuit.library import TwoLocal
        from qiskit_machine_learning.algorithms import VQC
        from qiskit_machine_learning.datasets import wine
        from qiskit_machine_learning.circuit.library import RawFeatureVector

        seed = 1376
        algorithm_globals.random_seed = seed

        # Use Wine data set for training and test data
        feature_dim = 4  # dimension of each data point
        training_size = 12
        test_size = 4

        # training features, training labels, test features, test labels as np.array,
        # one hot encoding for labels
        training_features, training_labels, test_features, test_labels = \
            wine(training_size=training_size, test_size=test_size, n=feature_dim)

        feature_map = RawFeatureVector(feature_dimension=feature_dim)
        ansatz = TwoLocal(feature_map.num_qubits, ['ry', 'rz'], 'cz', reps=3)
        vqc = VQC(feature_map=feature_map,
                  ansatz=ansatz,
                  optimizer=COBYLA(maxiter=100),
                  quantum_instance=QuantumInstance(BasicAer.get_backend('statevector_simulator'),
                                                   shots=1024,
                                                   seed_simulator=seed,
                                                   seed_transpiler=seed)
                  )
        vqc.fit(training_features, training_labels)

        score = vqc.score(test_features, test_labels)
        print('Testing accuracy: {:0.2f}'.format(score))

Further examples

Learning path notebooks may be found in the Machine Learning tutorials section of the documentation and are a great place to start.


Contribution Guidelines

If you'd like to contribute to Qiskit, please take a look at our contribution guidelines. This project adheres to Qiskit's code of conduct. By participating, you are expected to uphold this code.

We use GitHub issues for tracking requests and bugs. Please join the Qiskit Slack community and for discussion and simple questions. For questions that are more suited for a forum, we use the Qiskit tag in Stack Overflow.

Next Steps

Now you're set up and ready to check out some of the other examples from the Qiskit Tutorials repository, that are used for the IBM Quantum Experience.

Authors and Citation

Machine Learning was inspired, authored and brought about by the collective work of a team of researchers. Machine Learning continues to grow with the help and work of many people, who contribute to the project at different levels. If you use Qiskit, please cite as per the provided BibTeX file.

Please note that if you do not like the way your name is cited in the BibTex file then consult the information found in the .mailmap file.

License

This project uses the Apache License 2.0.

Comments
  • [WIP] Quantum CNN

    [WIP] Quantum CNN

    Summary

    Quantum CNN using torch connector following this tutorial.

    Details and comments

    @adekusar-drl @dlasecki Your help and feedback are much appreciated! This will fix #408 in future.

    Community PR 
    opened by Gopal-Dahale 18
  • Multi-class classification with qiskit's VQC

    Multi-class classification with qiskit's VQC

    I am following the tutorial given in qiskit's website Neural Network Classifier and Regressor. In the first part, classification, the third section refers to qiskit's VQC library. Everything works fine with the given X and y where there are only two classes. I modified the X and y slightly to include four classes instead of two using the following lines:

    num_inputs = 2
    num_samples = 100
    
    X = 2*np.random.rand(num_samples, num_inputs) - 1
    y = np.random.choice([0,1,2,3], 100)
    
    y_one_hot = np.zeros(( num_samples, 4 ))
    for i in range(len(y)):
        y_one_hot[i, y[i]]=1
    

    The rest of the code is untouched. VQC with ZZFeatureMap, RealAmplitudes ansatz, cross_entropy loss function and COBYLA() optimizer. However, when I try to fit with this new data, the classifier is only running for 5 iterations and the weights are not being changed at all. The loss or objection function's value is always calculated as "nan".

    There is a similar question I had posted about weights not being optimized with VQC, but then I thought it was because of my data or VQC's configuration. After trying this example, I realised it is clearly to do something with multiple classes and not just the classifier's configuration.

    Please shine light on how to do multi-class classification using the qiskit's VQC library.

    TLDR: VQC not optimizing weights if multi-class classification. Code: Colab Notebook

    opened by sri1905 18
  • Improve handling of missing optimizer in NN classifier/regressor

    Improve handling of missing optimizer in NN classifier/regressor

    What is the expected enhancement?

    When an optimizer is not passed to neural network classifier/regressor an exception occurs: AttributeError: 'NoneType' object has no attribute 'optimize'. In this issue I suggest to improve this behavior and either raise a more meaningful exception or introduce a default optimizer, e.g. Cobyla or SLSQP.

    good first issue priority: low type: enhancement 
    opened by adekusar-drl 18
  • Usage with qiskit runtime

    Usage with qiskit runtime

    What should we add?

    Since you're deprecating the runtime package, could you show how to use qiskit_ibm_runtime instead, e.g. adapting the tutorial already available in v0.4.0 doc ?

    type: feature request 
    opened by chin-jey 14
  • Missing instructions for building documentation locally

    Missing instructions for building documentation locally

    What is the expected enhancement?

    We should add instructions for building documentation locally for contributors who want to contribute on (non jupyter notebook) documentation such as doc string and .rst files.

    type: enhancement type: documentation 
    opened by HuangJunye 14
  • Batching of circuits to overcome memory issues when using statevector simulator

    Batching of circuits to overcome memory issues when using statevector simulator

    Summary

    Currently batch_size parameter is ignored if the statevector simulator is used. This leads to unreasonable memory use due to the size of the transpiled circuits, up to 1TB of RAM for 800 by 800 kernel matrix and 20 qubits (see qiskit-terra, issue #6991). This pull request fixes this by transpiling and simulating circuits in batches, never storing the entire 800 circuits. The modification uses batch_size parameter that is already used in non-statevector case.

    Details and comments

    I had success by setting batch_size=50 (memory footprint down to <20 GB).

    opened by rsln-s 14
  • QSVM Feature map

    QSVM Feature map

    Hi,

    I have question about QSVM. I did not know where to post it. Please be so kind to have a look at my question before closing it.

    QSVM is using the QunatumKernel class which is using a Quantum Feature Map (implemented as a Quantum circuit). So what really happens is that Feature Space (the higher dimensional space) is indeed constructed and used to transform the training data to a linearly separable space. So it is a kernel without the "kernel trick" or something like that?

    Thanks.

    type: question documentation 
    opened by toncho11 13
  • Clustering Algorithms using Quantum Computing

    Clustering Algorithms using Quantum Computing

    With regards to ML, Qiskit 0.30 provides Quantum Neural Networks, Neural Network Classifier, Quantum Kernel Machine Learning, qGANs for Loading Random Distributions and Torch Connector and Hybrid QNNs. I'm working to generate Clustering algorithms. At least k-means. Do you find it useful? Any suggestion?

    type: question 
    opened by dquero 13
  • This adds the callback feature for NN Classifier and Regressor (#112)

    This adds the callback feature for NN Classifier and Regressor (#112)

    Added 'callback` argument to NN classifier and NN Regressor.

    get_objective returns a callable which is passed as objective function to NN classifier.

    It checks for a callable callback argument, which if not None can access the intermediate data during the optimization.

    Summary

    This fixes #112.

    Details and comments

    • [x] Added callback argument to NeuralNetworkClassifier.
    • [x] Added callback argument to NeuralNetworkRegressor.
    • [x] Added wrapper get_objective method in TrainableModel which invokes ObjectiveFunction.objective() and callback()
    • [x] Passed the wrapped objective function as argument to Optimizer.optimize() in NeuralNetworkClassifier.
    • [x] Passed the wrapped objective function as argument to Optimizer.optimize() in NeuralNetworkClassifier.
    • [x] Added tests for testing callback in NeuralNetworkClassifier and NeuralNetworkRegressor.
    • [x] Added release note.
    Changelog: New Feature 
    opened by darshkaushik 13
  • Added getter/setter, exception handling for OpflowQNN, CircuitQNN

    Added getter/setter, exception handling for OpflowQNN, CircuitQNN

    Summary

    This PR closes #75:

    • Added getter and setter to OpflowQNN
    • In CircuitQNN, raises errors in methods for which a quantum_instance is required

    Details and comments

    My first PR in this repo, still figuring things out :)

    opened by stephenmurray2 13
  • Deprecate TorchRuntimeClient, corresponding classes, and the tutorial

    Deprecate TorchRuntimeClient, corresponding classes, and the tutorial

    What is the expected enhancement?

    The TorchRuntime program does not use primitives, so it will be removed soon. Before that we have to deprecate everything that is related to TorchRuntimeClient. This includes:

    • The runtime package with two scripts, hookbase.py and torch_runtime_client.py
    • The tutorial 06_torch_runtime.ipynb
    • Other components if any

    In case of the tutorial, I think, it is enough to add a text in bold in the beginning saying that the tutorial is deprecated, the corresponding runtime programs are deprecated and will be removed.

    type: enhancement 
    opened by adekusar-drl 12
  • QGAN with hybrid discriminator is not being trained.

    QGAN with hybrid discriminator is not being trained.

    Environment

    • Qiskit Machine Learning version: 0.5.0
    • Python version: 3.8.16
    • Operating system: Linux (Operating system)

    What is happening?

    I am training a quantum generative adversarial networks where the generator is classical and the discriminator is hybrid quantum-classical network. For building such circuit, I have used the idea of hybrid quantum-classical neural network from the tutorial of qiskit link. But when I am training the QGAN, I am seeing no progress in the training bar, which means the network is not being trained.

    How can we reproduce the issue?

    The function of quantum circuit is shown as :

    def create_qnn():
      feature_map = ZZFeatureMap(16)
      ansatz = RealAmplitudes(16, reps=1)
      qc = QuantumCircuit(16)
      qc.compose(feature_map, inplace=True)
      qc.compose(ansatz, inplace=True)
    
      qnn = EstimatorQNN(
          circuit=qc,
          input_params = feature_map.parameters,
          weight_params = ansatz.parameters,
          input_gradients=True
      )
    
      return qnn
    
    qnn1 = create_qnn()
    

    The discriminator network (hybrid quantum-classical neural network) is shown as:

    class Discriminator(nn.Module):
        def __init__(self, qnn):
            super(Discriminator, self).__init__()
            self.n_input = 784
            self.main = nn.Sequential(
                nn.Linear(self.n_input, 1024),
                nn.LeakyReLU(0.2),
                nn.Dropout(0.3),
                nn.Linear(1024, 512),
                nn.LeakyReLU(0.2),
                nn.Dropout(0.3),
                nn.Linear(512, 256),
                nn.LeakyReLU(0.2),
                nn.Dropout(0.3),
                nn.Linear(256, 16),
                TorchConnector(qnn),
                nn.Linear(1, 1),
                nn.Sigmoid(),
            )
        def forward(self, x):
            x = x.view(-1, 784)
            return self.main(x)
    

    The training section of discriminator is shown as:

    def train_discriminator(optimizer, data_real, data_fake):
        b_size = data_real.size(0)
        real_label = label_real(b_size)
        fake_label = label_fake(b_size)
        optimizer.zero_grad()
        output_real = discriminator(data_real)
        loss_real = criterion(output_real, real_label)
        output_fake = discriminator(data_fake)
        loss_fake = criterion(output_fake, fake_label)
        loss_real.backward()
        loss_fake.backward()
        optimizer.step()
        return loss_real + loss_fake
    

    What should happen?

    The network should simply train, which in my case is not happening.

    Any suggestions?

    I believe that in the tutorial of QGAN link has used hybrid quantum-classical neural network for generator, but they have also created a custom function for gradient-based training for the generator. I assume here, in my issue, as well I need to create such function, but I am not getting it somehow. Could someone help me figure out the same?

    The training starts as I replace the BATCH_SIZE to 1 and have changed the number of qubits in the quantum circuit from 16 to 2. What does it mean? Is there any limitations to TorchConnector class regards to number of qubits it can operate, or is it because of the size of the dataset that I am using during the time of training i.e. Batch size ?

    opened by Shuhul24 0
  • Add StatevectorKernel

    Add StatevectorKernel

    Summary

    Adds a StatevectorKernel class implementation that has been optimized for use with statevector-implemented feature maps. Computed statevector arrays are also stored in a statevector cache to avoid repeated computation.

    The performance of this implementation should be closer to the previous implementation of QuantumKernel running on a statevector simulator. Thus complexity is reduced from O(N^2) to O(N).

    Details and comments

    ~~Currently, the implementation includes an enforce_psd argument in conjunction with FidelityQuantumKernel, however, I'm fairly positive that the kernel matrix is guaranteed to be PSD when using the statevector-based feature mapping. If this is confirmed, I will remove this argument and functionality.~~ Removed.

    opened by declanmillar 3
  • The new FidelityQuantumKernel is 50 times slower than the original QuantumKernel!

    The new FidelityQuantumKernel is 50 times slower than the original QuantumKernel!

    We have been trying to port over to the new FidelityQuantumKernel as QuantumKernel is deprecated. However it is 50X slower than QuantumKernel with basic usage! This basically makes it completely unusable.

    This is with the latest qiskit and qiskit-machine-learning:

    qiskit==0.39.4 qiskit-machine-learning==0.5.0

    and Python 3.10.4, on Ubuntu 20.04.3 LTS.

    Am I missing something?

    Here is the code:

    from qiskit import BasicAer
    from qiskit.circuit.library import PauliFeatureMap
    from qiskit.utils import QuantumInstance
    from qiskit_machine_learning.kernels.quantum_kernel import QuantumKernel
    from qiskit_machine_learning.kernels import FidelityQuantumKernel
    
    import numpy as np
    
    import time
    
    
    feature_dim = 5
    n = 100
    X = np.random.randn(n, feature_dim)
    
    
    paulis = ("X", "YZ")
    reps = 2
    entanglement = "full"
    
    
    quantum_instance = QuantumInstance(
        BasicAer.get_backend("statevector_simulator"),
        shots=1024,
        seed_simulator=0,
        seed_transpiler=0,
    )
    
    feature_map = PauliFeatureMap(
        feature_dimension=feature_dim,
        alpha=0.5,
        reps=reps,
        paulis=paulis,
        entanglement=entanglement,
    )
    kernel = QuantumKernel(feature_map=feature_map, quantum_instance=quantum_instance)
    
    
    start = time.time()
    K = kernel.evaluate(X, X)
    end = time.time()
    
    etime_orig = end - start
    print(f"Original kernel elapsed time: {etime_orig/60.0} mins.")
    
    new_kernel = FidelityQuantumKernel(feature_map=feature_map)
    
    start = time.time()
    K_new = new_kernel.evaluate(X, X)
    end = time.time()
    
    etime_new = end - start
    print(f"New kernel elapsed time: {etime_new/60.0} mins.")
    
    print(etime_new / etime_orig)
    
    

    And results:

    Original kernel elapsed time: 0.04224474827448527 mins.
    
    New kernel elapsed time: 2.15045348405838 mins.
    
    50.904634821961956
    
    opened by brian36 3
  • Improved content for Tutorial 5 (QAMP fall'22)

    Improved content for Tutorial 5 (QAMP fall'22)

    Summary

    Enhancing the documentation of Tutorial "05_torch_connector" as a mentee for QAMP 2022. Mentor: @ElePT Mentee: @SanyaNanda

    Details and comments

    Previous PR: https://github.com/Qiskit/qiskit-machine-learning/pull/502

    opened by SanyaNanda 2
  • Improved content for Tutorial 1 (QAMP fall'22)

    Improved content for Tutorial 1 (QAMP fall'22)

    Summary

    Enhancing the documentation of Tutorial 01, as a mentee for QAMP 2022. Mentor: @ElePT Mentee: @SanyaNanda

    Details and comments

    Previous PR: https://github.com/Qiskit/qiskit-machine-learning/pull/473

    QAMP 
    opened by SanyaNanda 3
  • Improved content for Tutorial 9 (QAMP fall'22)

    Improved content for Tutorial 9 (QAMP fall'22)

    Summary

    Enhancing the documentation of Tutorial "09_saving_loading" as a mentee for QAMP 2022. Mentor: @ElePT Mentee: @SanyaNanda

    Details and comments

    Restructured the notebook (Overview, Introduction, Objective, Tutorial, accuracy table and what was learned) Imports placed where required Added more explanations and inline comments for better understanding of the code Hyperlinks to API references wherever required

    opened by SanyaNanda 1
Releases(0.5.0)
  • 0.5.0(Nov 8, 2022)

    Changelog

    New Features

    • Added support for categorical and ordinal labels to VQC. Now labels can be passed in different formats, they can be plain ordinal labels, a one dimensional array that contains integer labels like 0, 1, 2, …, or an array with categorical string labels. One-hot encoded labels are still supported. Internally, labels are transformed to one hot encoding and the classifier is always trained on one hot labels.

    • Introduced Estimator Quantum Neural Network (EstimatorQNN) based on (runtime) primitives. This implementation leverages the estimator primitive (see BaseEstimator) and the estimator gradients (see BaseEstimatorGradient) to enable runtime access and more efficient computation of forward and backward passes. The new EstimatorQNN exposes a similar interface to the Opflow QNN, with a few differences. One is the quantum_instance parameter. This parameter does not have a direct replacement, and instead the estimator parameter must be used. The gradient parameter keeps the same name as in the Opflow QNN implementation, but it no longer accepts Opflow gradient classes as inputs; instead, this parameter expects an (optionally custom) primitive gradient. The existing training algorithms such as VQR, that were based on the Opflow QNN, are updated to accept both implementations. The implementation of NeuralNetworkRegressor has not changed.

    • Introduced Quantum Kernels based on (runtime) primitives. This implementation leverages the fidelity primitive (see BaseStateFidelity) and provides more flexibility to end users. The fidelity primitive calculates state fidelities/overlaps for pairs of quantum circuits and requires an instance of Sampler. Thus, users may plug in their own implementations of fidelity calculations. The new kernels expose the same interface and the same parameters except the quantum_instance parameter. This parameter does not have a direct replacement and instead the fidelity parameter must be used.

      A new hierarchy is introduced:

            - A base and abstract class [BaseKernel](https://qiskit.org/documentation/machine-learning/stubs/qiskit_machine_learning.kernels.BaseKernel.html#qiskit_machine_learning.kernels.BaseKernel) is introduced. All concrete implementation must inherit this class.
      
            - A fidelity based quantum kernel [FidelityQuantumKernel](https://qiskit.org/documentation/machine-learning/stubs/qiskit_machine_learning.kernels.FidelityQuantumKernel.html#qiskit_machine_learning.kernels.FidelityQuantumKernel) is added. This is a direct replacement of [QuantumKernel](https://qiskit.org/documentation/machine-learning/stubs/qiskit_machine_learning.kernels.QuantumKernel.html#qiskit_machine_learning.kernels.QuantumKernel). The difference is that the new class takes either a sampler or a fidelity instance to estimate overlaps and construct kernel matrix.
      
            - A new abstract class [TrainableKernel](https://qiskit.org/documentation/machine-learning/stubs/qiskit_machine_learning.kernels.TrainableKernel.html#qiskit_machine_learning.kernels.TrainableKernel) is introduced to generalize ability to train quantum kernels.
      
            - A fidelity-based trainable quantum kernel [TrainableFidelityQuantumKernel](https://qiskit.org/documentation/machine-learning/stubs/qiskit_machine_learning.kernels.TrainableFidelityQuantumKernel.html#qiskit_machine_learning.kernels.TrainableFidelityQuantumKernel) is introduced. This is a replacement of the existing [QuantumKernel](https://qiskit.org/documentation/machine-learning/stubs/qiskit_machine_learning.kernels.QuantumKernel.html#qiskit_machine_learning.kernels.QuantumKernel) if a trainable kernel is required. The trainer [QuantumKernelTrainer](https://qiskit.org/documentation/machine-learning/stubs/qiskit_machine_learning.kernels.algorithms.QuantumKernelTrainer.html#qiskit_machine_learning.kernels.algorithms.QuantumKernelTrainer) now accepts both quantum kernel implementations, the new one and the existing one.
      

      The existing algorithms such as QSVC, QSVR and other kernel-based algorithms are updated to accept both implementations.

    • Introduced Sampler Quantum Neural Network (SamplerQNN) based on (runtime) primitives. This implementation leverages the sampler primitive (see BaseSampler) and the sampler gradients (see BaseSamplerGradient) to enable runtime access and more efficient computation of forward and backward passes more efficiently. The new SamplerQNN exposes a similar interface to the CircuitQNN, with a few differences. One is the quantum_instance parameter. This parameter does not have a direct replacement, and instead the sampler parameter must be used. The gradient parameter keeps the same name as in the CircuitQNN implementation, but it no longer accepts Opflow gradient classes as inputs; instead, this parameter expects an (optionally custom) primitive gradient. The sampling option has been removed for the time being, as this information is not currently exposed by the Sampler, and might correspond to future lower-level primitives.

    • The existing training algorithms such as VQC, that were based on the CircuitQNN, are updated to accept both implementations. The implementation of NeuralNetworkClassifier has not changed.

    • Expose the callback attribute as public property on TrainableModel. This, for instance, allows setting the callback between optimizations and store the history in separate objects.

    • Gradient operator/circuit initialization in OpflowQNN and CircuitQNN respectively is now delayed until the first call of the backward method. Thus, the networks are created faster and gradient framework objects are not created until they are required.

    • Introduced a new parameter evaluate_duplicates in QuantumKernel. This parameter defines a strategy how kernel matrix elements are evaluated if duplicate samples are found. Possible values are:

            -  all means that all kernel matrix elements are evaluated, even the diagonal ones when
      
                training. This may introduce additional noise in the matrix.
      
            - off_diagonal when training the matrix diagonal is set to 1, the rest elements are
      
                fully evaluated, e.g., for two identical samples in the dataset. When inferring, all elements are evaluated. This is the default value.
      
            - none when training the diagonal is set to 1 and if two identical samples are found
      
                in the dataset the corresponding matrix element is set to 1. When inferring, matrix elements for identical samples are set to 1.
      
    • In the previous releases, in the QGAN class, the gradient penalty could not be enabled to train the discriminator with a penalty function. Thus, a gradient penalty parameter was added during the initialization of the QGAN algorithm. This parameter indicates whether or not penalty function is applied to the loss function of the discriminator during training.

    • Enable the default construction of the ZFeatureMap in the TwoLayerQNN if the number of qubits is 1. Previously, not providing a feature map for the single qubit case raised an error as default construction assumed 2 or more qubits.

    • VQC will now raise an error when training from a warm start when encountering targets with a different number of classes to the previous dataset.

    • VQC will now raise an error when a user attempts multi-label classification, which is not supported.

    • Added two new properties to the TrainableModel class:

            - fit_result returns a resulting object from the optimization procedure. Please refers to the Terra’s documentation of the OptimizerResult class.
            - weights returns an array of trained weights, this is a convenient way to get access to the weights, it is the same as calling model.fit_result.x.
      

    Upgrade Notes

    • The method fit() is not abstract any more. Now, it implements basic checks, calls a new abstract method _fit_internal() to be implemented by sub-classes, and keeps track of fit_result property that is returned by this new abstract method. Thus, any sub-class of TrainableModel must implement this new method. Classes NeuralNetworkClassifier and NeuralNetworkRegressor have been updated correspondingly.

    • Inheriting from sklearn.svm.SVC in PegasosQSVC resulted in errors when calling some inherited methods such as decision_function due to the overridden fit implementation. For that reason, the inheritance has been replaced by a much lighter inheritance from ClassifierMixin providing the score method and a new method decision_function has been implemented. The class is still sklearn compatible due to duck typing. This means that for the user everything that has been working in the previous release still works, except the inheritance. The only methods that are no longer supported (such as predict_proba) were only raising errors in the previous release in practice.

    Deprecation Notes

    Bug Fixes

    • Previously in the QuantumGenerator of the QGAN algorithm, if we used a simulator other than the statevector_simulator the result dictionary had not the correct size to compute both the gradient and the loss functions. Now, the values output are stored in a vector of size 2^n and each key is mapped to its value from the result dictionary in the new value array. Also, each key is stored in a vector of size 2^n where each element of the vector keys[i] corresponds to the binary representation of i.

    • Previously in the QuantumGenerator of the QGAN algorithm, the gradients were computed using the statevector backend even if we specified another backend. To solve this issue, the gradient object is converted into a CircuitStateFn instead of its adjoint as in the previous version. The gradients are converted into the backend-dependent structure using CircuitSampler. After the evaluation of the object, the gradient_function is stored in a dense array to fix a dimension incompatibility when computing the loss function.

    • Fixed quantum kernel evaluation when duplicate samples are found in the dataset. Originally, kernel matrix elements were not evaluated for identical samples in the dataset and such elements were set wrongly to zero. Now we introduced a new parameter evaluate_duplicates that ensures that elements of the kernel matrix are evaluated correctly. See the feature section for more details.

    • Previously in the pytorch_discriminator class of the QGAN algorithm, if the gradient penalty parameter was enabled, the latent variable z was not properly initialized : Variable module was used instead of torch.autograd.Variable.

    • Calling PegasosQSVC.decision_function() raises an error. Fixed by writing own method instead of inheriting from SVC. The inheritance from SVC in the PegasosQSVC class is removed. To keep the score method, inheritance to the mixin class ClassifierMixin from scikit-learn is added.

    Source code(tar.gz)
    Source code(zip)
  • 0.4.0(Apr 29, 2022)

    Changelog

    New Features

    • In the previous releases at the backpropagation stage of CircuitQNN and OpflowQNN gradients were computed for each sample in a dataset individually and then the obtained values were aggregated into one output array. Thus, for each sample in a dataset at least one job was submitted. Now, gradients are computed for all samples in a dataset in one go by passing a list of values for a single parameter to CircuitSampler. Therefore, a number of jobs required for such computations is significantly reduced. This improvement may speed up training process in the cloud environment, where queue time for submitting a job may be a major contribution in the overall training time.

    • Introduced two new classes, EffectiveDimension and LocalEffectiveDimension, for calculating the capacity of quantum neural network models through the computation of the Fisher Information Matrix. The local effective dimension bounds the generalization error of QNNs and only accepts single parameter sets as inputs. The global effective dimension (or just effective dimension) can be used as a measure of the expressibility of the model, and accepts multiple parameter sets.

    • Objective functions constructed by the neural network classifiers and regressors now include an averaging factor that is evaluated as 1 / number_of_samples. Computed averaged objective values are passed to a user specified callback if any. Users may notice a dramatic decrease in the objective values in their callbacks. This is due to this averaging factor.

    • Added support for saving and loading machine learning models. This support is introduced in TrainableModel, so all sub-classes can be saved and loaded. Also, kernel based models can be saved and loaded. A list of models that support saving and loading models:

        NeuralNetworkClassifier
      
        NeuralNetworkRegressor
      
        VQC
      
        VQR
      
        QSVC
      
        QSVR
      
        PegasosQSVC
      
    • When model is saved all model parameters are saved to a file, including a quantum instance that is referenced by internal objects. That means if a model is loaded from a file and is used, for instance, for inference, the same quantum instance and a corresponding backend will be used even if a cloud backend was used.

    • Added a new feature in CircuitQNN that ensures unbound_pass_manager is called when caching the QNN circuit and that bound_pass_manager is called when QNN parameters are assigned.

    • Added a new feature in QuantumKernel that ensures the bound_pass_manager is used, when provided via the QuantumInstance, when transpiling the kernel circuits.

    Upgrade Notes

    • Added support for running with Python 3.10. At the the time of the release, Torch didn’t have a python 3.10 version.

    • The previously deprecated BaseBackend class has been removed. It was originally deprecated in the Qiskit Terra 0.18.0 release.

    • Support for running with Python 3.6 has been removed. To run Machine Learning you need a minimum Python version of 3.7.

    Deprecation Notes

    • The functions breast_cancer, digits, gaussian, iris and wine in the datasets module are deprecated and should not be used.

    • Class CrossEntropySigmoidLoss is deprecated and marked for removal.

    • Removed support of l2 and l1 values as loss function definitions. Please, use absolute_error and squared_error respectively.

    Bug Fixes

    • Fixes in Ad Hoc dataset. Fixed an ValueError when n=3 is passed to ad_hoc_data. When the value of n is not 2 or 3, a ValueError is raised with a message that the only supported values of n are 2 and 3.

    • Previously, VQC would throw an error if trained on batches of data where not all of the target labels that can be found in the full dataset were present. This is because VQC interpreted the number of unique targets in the current batch as the number of classes. Currently, VQC is hard-coded to expect one-hot-encoded targets. Therefore, VQC will now determine the number of classes from the shape of the target array.

    • Fixes an issue where VQC could not be trained on multiclass datasets. It returned nan values on some iterations. This is fixed in 2 ways. First, the default parity function is now guaranteed to be able to assign at least one output bitstring to each class, so long as 2**N >= C where N is the number of output qubits and C is the number of classes. This guarantees that it is at least possible for every class to be predicted with a non-zero probability. Second, even with this change it is still possible that on a given training instance a class is predicted with 0 probability. Previously this could lead to nan in the CrossEntropyLoss calculation. We now replace 0 probabilities with a small positive value to ensure the loss cannot return nan.

    • Fixes an issue in QuantumKernel where evaluating a quantum kernel for data with dimension d>2 raised an error. This is fixed by changing the hard-coded reshaping of one-dimensional arrays in QuantumKernel.evaluate().

    • Fixes an issue where VQC would fail with warm_start=True. The extraction of the initial_point in TrainableModel from the final point of the minimization had not been updated to reflect the refactor of optimizers in qiskit-terra; the old optimize method, that returned a tuple was deprecated and new method minimize was created that returns an OptimizerResult object. We now correctly recover the final point of the minimization from previous fits to use for a warm start in subsequent fits.

    • Added GPU support to TorchConnector. Now, if a hybrid PyTorch model is being trained on GPU, TorchConnector correctly detaches tensors, moves them to CPU, evaluate forward and backward passes and places resulting tensors to the same device they came from.

    • Fixed a bug when a sparse array is passed to VQC as labels. Sparse arrays can be easily observed when labels are encoded via OneHotEncoder from SciKit-Learn. Now both NeuralNetworkClassifier and VQC support sparse arrays and convert them dense arrays in the implementation.

    Source code(tar.gz)
    Source code(zip)
  • 0.3.1(Feb 17, 2022)

    Changelog

    Upgrade Notes

    • Added support for running with Python 3.10. At the the time of the release, Torch didn’t have a python 3.10 version.

    Bug Fixes

    • Fixes in Ad Hoc dataset. Fixed an ValueError when n=3 is passed to ad_hoc_data. When the value of n is not 2 or 3, a ValueError is raised with a message that the only supported values of n are 2 and 3.

    • Previously, VQC would throw an error if trained on batches of data where not all of the target labels that can be found in the full dataset were present. This is because VQC interpreted the number of unique targets in the current batch as the number of classes. Currently, VQC is hard-coded to expect one-hot-encoded targets. Therefore, VQC will now determine the number of classes from the shape of the target array.

    • Fixes an issue where VQC could not be trained on multiclass datasets. It returned nan values on some iterations. This is fixed in 2 ways. First, the default parity function is now guaranteed to be able to assign at least one output bitstring to each class, so long as 2**N >= C where N is the number of output qubits and C is the number of classes. This guarantees that it is at least possible for every class to be predicted with a non-zero probability. Second, even with this change it is still possible that on a given training instance a class is predicted with 0 probability. Previously this could lead to nan in the CrossEntropyLoss calculation. We now replace 0 probabilities with a small positive value to ensure the loss cannot return nan.

    • Fixes an issue where VQC would fail with warm_start=True. The extraction of the initial_point in TrainableModel from the final point of the minimization had not been updated to reflect the refactor of optimizers in qiskit-terra; the old optimize method, that returned a tuple was deprecated and new method minimize was created that returns an OptimizerResult object. We now correctly recover the final point of the minimization from previous fits to use for a warm start in subsequent fits.

    Source code(tar.gz)
    Source code(zip)
  • 0.3.0(Dec 15, 2021)

    Changelog

    New Features

    • Addition of a QuantumKernelTrainer object which may be used by kernel-based machine learning algorithms to perform optimization of some QuantumKernel parameters before training the model. Addition of a new base class, KernelLoss, in the loss_functions package. Addition of a new KernelLoss subclass, SVCLoss.

    • The class TrainableModel, and its sub-classes NeuralNetworkClassifier, NeuralNetworkRegressor, VQR, VQC, have a new optional argument callback. User can optionally provide a callback function that can access the intermediate training data to track the optimization process, else it defaults to None. The callback function takes in two parameters: the weights for the objective function and the computed objective value. For each iteration an optimizer invokes the callback and passes current weights and computed value of the objective function.

    • Classification models (i.e. models that extend the NeuralNetworkClassifier class like VQC) can now handle categorical target data in methods like fit() and score(). Categorical data is inferred from the presence of string type data and is automatically encoded using either one-hot or integer encodings. Encoder type is determined by the one_hot argument supplied when instantiating the model.

    • There’s an additional transpilation step introduced in CircuitQNN that is invoked when a quantum instance is set. A circuit passed to CircuitQNN is transpiled and saved for subsequent usages. So, every time when the circuit is executed it is already transpiled and overall time of the forward pass is reduced. Due to implementation limitations of RawFeatureVector it can’t be transpiled in advance, so it is transpiled every time it is required to be executed and only when all parameters are bound. This means overall performance when RawFeatureVector is used stays the same.

    • Introduced a new classification algorithm, which is an alternative version of the Quantum Support Vector Classifier (QSVC) that is trained via the Pegasos algorithm from https://home.ttic.edu/~nati/Publications/PegasosMPB.pdf instead of the dual optimization problem like in sklearn. This algorithm yields a training complexity that is independent of the size of the training set (see the to be published Master’s Thesis “Comparing Quantum Neural Networks and Quantum Support Vector Machines” by Arne Thomsen), such that the PegasosQSVC is expected to train faster than QSVC for sufficiently large training sets.

    • QuantumKernel transpiles all circuits before execution. However, this information was not being passed, which calls the transpiler many times during the execution of the QSVC/QSVR algorithm. Now, had_transpiled=True is passed correctly and the algorithm runs faster.

    • QuantumKernel now provides an interface for users to specify a new class field, user_parameters. User parameters are an array of Parameter objects corresponding to parameterized quantum gates in the feature map circuit the user wishes to tune. This is useful in algorithms where feature map parameters must be bound and re-bound many times (i.e. variational algorithms). Users may also use a new function assign_user_parameters to assign real values to some or all of the user parameters in the feature map.

    • Introduce the TorchRuntimeClient for training a quantum model or a hybrid quantum-classical model faster using Qiskit Runtime. It can also be used for predicting the result using the trained model or calculating the score of the trained model faster using Qiskit Runtime.

    Known Issues

    • If positional arguments are passed into QSVR or QSVC and these classes are printed, an exception is raised.

    Deprecation Notes

    • Positional arguments in QSVR and QSVC are deprecated.

    Bug Fixes

    • Fixed a bug in QuantumKernel where for statevector simulator all circuits were constructed and transpiled at once, leading to high memory usage. Now the circuits are batched similarly to how it was previously done for non-statevector simulators (same flag is used for both now; previously batch_size was silently ignored by statevector simulator)

    • Fix a bug where TorchConnector failed on backward pass computation due to empty parameters for inputs or weights. Validation added to qiskit_machine_learning.neural_networks.NeuralNetwork._validate_backward_output().

    • TwoLayerQNN now passes the value of the exp_val parameter in the constructor to the constructor of OpflowNN which TwoLayerQNN inherits from.

    • In some configurations forward pass of a neural network may return the same value across multiple calls even if different weights are passed. This behavior is confirmed with AQGD optimizer. This was due to a bug in the implementation of the objective functions. They cache a value obtained at the forward pass to be re-used in the backward pass. Initially, this cache was based on an identifier (a call of id() function) of the weights array. AQGD re-uses the same array for weights: it updates the values keeping an instance of the array the same. This caused to re-use the same forward pass value across all iteration. Now the forward pass cache is based on actual values of weights instead of identifiers.

    • Fix a bug, where qiskit_machine_learning.circuit.library.RawFeatureVector.copy() didn’t copy all internal settings which could lead to issues with the copied circuit. As a consequence qiskit_machine_learning.circuit.library.RawFeatureVector.bind_parameters() is also fixed.

    • Fixes a bug where VQC could not be instantiated unless either feature_map or ansatz were provided (#217). VQC is now instantiated with the default feature_map and/or ansatz.

    • The QNN weight parameter in TorchConnector is now registered in the torch DAG as weight, instead of _weights. This is consistent with the PyTorch naming convention and the weight property used to get access to the computed weights.

    Source code(tar.gz)
    Source code(zip)
  • 0.2.1(Aug 24, 2021)

    Changelog

    Added

    • The class TrainableModel, and its sub-classes NeuralNetworkClassifier, NeuralNetworkRegressor, VQR, VQC, have a new optional argument callback. User can optionally provide a callback function that can access the intermediate training data to track the optimization process, else it defaults to None. The callback function takes in two parameters: the weights for the objective function and the computed objective value. For each iteration an optimizer invokes the callback and passes current weights and computed value of the objective function.
    • Classification models (i.e. models that extend the NeuralNetworkClassifier class like VQC) can now handle categorical target data in methods like fit() and score(). Categorical data is inferred from the presence of string type data and is automatically encoded using either one-hot or integer encodings. Encoder type is determined by the one_hot argument supplied when instantiating the model.

    Fixed

    • Fix a bug, where qiskit_machine_learning.circuit.library.RawFeatureVector.copy() didn’t copy all internal settings which could lead to issues with the copied circuit. As a consequence qiskit_machine_learning.circuit.library.RawFeatureVector.bind_parameters() is also fixed.

    • The QNN weight parameter in TorchConnector is now registered in the torch DAG as weight, instead of _weights. This is consistent with the PyTorch naming convention and the weight property used to get access to the computed weights.

    Source code(tar.gz)
    Source code(zip)
  • 0.2.0(Jul 12, 2021)

    Changelog

    Added

    • A base class TrainableModel is introduced for machine learning models. This class follows Scikit-Learn principles and makes the quantum machine learning compatible with classical models. Both NeuralNetworkClassifier and NeuralNetworkRegressor extend this class. A base class ObjectiveFunction is introduced for objective functions optimized by machine learning models. There are three objective functions introduced that are used by ML models: BinaryObjectiveFunction, MultiClassObjectiveFunction, and OneHotObjectiveFunction. These functions are used internally by the models.
    • The optimizer argument for the classes NeuralNetworkClassifier and NeuralNetworkRegressor, both of which extends the TrainableModel class, is made optional with the default value being SLSQP(). The same is true for the classes VQC and VQR as they inherit from NeuralNetworkClassifier and NeuralNetworkRegressor respectively.
    • The constructor of NeuralNetwork, and all classes that inherit from it, has a new parameter input_gradients which defaults to False. Previously this parameter could only be set using the setter method. Note that TorchConnector previously set input_gradients of the NeuralNetwork it was instantiated with to True. This is not longer the case. So if you use TorchConnector and want to compute the gradients w.r.t. the input, make sure you set input_gradients=True on the NeuralNetwork before passing it to TorchConnector.
    • Added a parameter initial_point to the neural network classifiers and regressors. This an array that is passed to an optimizer as an initial point to start from.
    • Computation of gradients with respect to input data in the backward method of NeuralNetwork is now optional. By default gradients are not computed. They may inspected and turned on, if required, by getting or setting a new property input_gradients in the NeuralNetwork class.
    • Now NeuralNetworkClassifier extends ClassifierMixin and NeuralNetworkRegressor extends RegressorMixin from Scikit-Learn and rely on their methods for score calculation. This also adds an ability to pass sample weights as an optional parameter to the score methods.

    Changed

    • The valid values passed to the loss argument of the TrainableModel constructor were partially deprecated (i.e. loss='l1' is replaced with loss='absolute_error' and loss='l2' is replaced with loss='squared_error'). This affects instantiation of classes like the NeuralNetworkClassifier. This change was made to reduce confusion that stems from using lowercase ‘l’ character which can be mistaken for a numeric ‘1’ or capital ‘I’. You should update your model instantiations by replacing ‘l1’ with ‘absolute_error’ and ‘l2’ with ‘squared_error’.
    • The weights property in TorchConnector is deprecated in favor of the weight property which is PyTorch compatible. By default, PyTorch layers expose weight properties to get access to the computed weights.

    Fixed

    • This fixes the exception that occurs when no optimizer argument is passed to NeuralNetworkClassifier and NeuralNetworkRegressor.
    • Fixes the computation of gradients in TorchConnector when a batch of input samples is provided.
    • TorchConnector now returns the correct input gradient dimensions during the backward pass in hybrid nn training.
    • Added a dedicated handling of ComposedOp as a operator in OpflowQNN. In this case output shape is determined from the first operator in the ComposedOp instance.
    • Fix the dimensions of the gradient in the quantum generator for the qGAN training.
    Source code(tar.gz)
    Source code(zip)
  • 0.1.0(Apr 1, 2021)

Owner
Qiskit
An open-source SDK for working with quantum computers at the level of pulses, circuits, and algorithms.
Qiskit
Machine Learning for RC Cars

Suiron Machine Learning for RC Cars Prediction visualization (green = actual, blue = prediction) Click the video below to see it in action! Dependenci

Kendrick Tan 706 Jan 02, 2023
Datetimes for Humans™

Maya: Datetimes for Humans™ Datetimes are very frustrating to work with in Python, especially when dealing with different locales on different systems

Timo Furrer 3.4k Dec 28, 2022
Learning --> Numpy January 2022 - winter'22

Numerical-Python Numpy NumPy is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along

Shahzaneer Ahmed 0 Mar 12, 2022
Massively parallel self-organizing maps: accelerate training on multicore CPUs, GPUs, and clusters

Somoclu Somoclu is a massively parallel implementation of self-organizing maps. It exploits multicore CPUs, it is able to rely on MPI for distributing

Peter Wittek 239 Nov 10, 2022
Machine Learning Techniques using python.

👋 Hi, I’m Fahad from TEXAS TECH. 👀 I’m interested in Optimization / Machine Learning/ Statistics 🌱 I’m currently learning Machine Learning and Stat

FAHAD MOSTAFA 1 Jan 19, 2022
Bodywork deploys machine learning projects developed in Python, to Kubernetes.

Bodywork deploys machine learning projects developed in Python, to Kubernetes. It helps you to: serve models as microservices execute batch jobs run r

Bodywork Machine Learning 409 Jan 01, 2023
SageMaker Python SDK is an open source library for training and deploying machine learning models on Amazon SageMaker.

SageMaker Python SDK SageMaker Python SDK is an open source library for training and deploying machine learning models on Amazon SageMaker. With the S

Amazon Web Services 1.8k Jan 01, 2023
机器学习检测webshell

ai-webshell-detect 机器学习检测webshell,利用textcnn+简单二分类网络,基于keras,花了七天 检测原理: 从文件熵 文件长度 文件语句提取出特征,然后文件熵与长度送入二分类网络,文件语句送入textcnn 项目原理,介绍,怎么做出来的

Huoji's 56 Dec 14, 2022
A linear regression model for house price prediction

Linear_Regression_Model A linear regression model for house price prediction. This code is using these packages, so please make sure your have install

ShawnWang 1 Nov 29, 2021
A framework for building (and incrementally growing) graph-based data structures used in hierarchical or DAG-structured clustering and nearest neighbor search

A framework for building (and incrementally growing) graph-based data structures used in hierarchical or DAG-structured clustering and nearest neighbor search

Nicholas Monath 31 Nov 03, 2022
Graphsignal is a machine learning model monitoring platform.

Graphsignal is a machine learning model monitoring platform. It helps ML engineers, MLOps teams and data scientists to quickly address issues with data and models as well as proactively analyze model

Graphsignal 143 Dec 05, 2022
STUMPY is a powerful and scalable Python library for computing a Matrix Profile, which can be used for a variety of time series data mining tasks

STUMPY STUMPY is a powerful and scalable library that efficiently computes something called the matrix profile, which can be used for a variety of tim

TD Ameritrade 2.5k Jan 06, 2023
SynapseML - an open source library to simplify the creation of scalable machine learning pipelines

Synapse Machine Learning SynapseML (previously MMLSpark) is an open source library to simplify the creation of scalable machine learning pipelines. Sy

Microsoft 3.9k Dec 30, 2022
inding a method to objectively quantify skill versus chance in games, using reinforcement learning

Skill-vs-chance-games-analysis - Finding a method to objectively quantify skill versus chance in games, using reinforcement learning

Marcus Chiam 4 Nov 19, 2022
A repository for collating all the resources such as articles, blogs, papers, and books related to Bayesian Statistics.

A repository for collating all the resources such as articles, blogs, papers, and books related to Bayesian Statistics.

Aayush Malik 80 Dec 12, 2022
A basic Ray Tracer that exploits numpy arrays and functions to work fast.

Python-Fast-Raytracer A basic Ray Tracer that exploits numpy arrays and functions to work fast. The code is written keeping as much readability as pos

Rafael de la Fuente 393 Dec 27, 2022
In this Repo a simple Sklearn Model will be trained and pushed to MLFlow

SKlearn_to_MLFLow In this Repo a simple Sklearn Model will be trained and pushed to MLFlow Install This Repo is based on poetry python3 -m venv .venv

1 Dec 13, 2021
machine learning model deployment project of Iris classification model in a minimal UI using flask web framework and deployed it in Azure cloud using Azure app service

This is a machine learning model deployment project of Iris classification model in a minimal UI using flask web framework and deployed it in Azure cloud using Azure app service. We initially made th

Krishna Priyatham Potluri 73 Dec 01, 2022
Little Ball of Fur - A graph sampling extension library for NetworKit and NetworkX (CIKM 2020)

Little Ball of Fur is a graph sampling extension library for Python. Please look at the Documentation, relevant Paper, Promo video and External Resour

Benedek Rozemberczki 619 Dec 14, 2022
Machine Learning Model to predict the payment date of an invoice when it gets created in the system.

Payment-Date-Prediction Machine Learning Model to predict the payment date of an invoice when it gets created in the system.

15 Sep 09, 2022