TensorFlow Quantum (TFQ) Documentation

repository·master·Indexed 24 days ago

https://github.com/tensorflow/quantum

A high-performance Python framework for hybrid quantum-classical machine learning. TFQ integrates quantum circuit simulations via Cirq and qsim directly into TensorFlow and Keras computational graphs. It provides tools for modeling quantum data, automatic differentiation of quantum circuits using methods like parameter shift, and specialized primitives for quantum circuits and Pauli sums.

Tokens
10.5K
Snippets
29
Records
50
Agent score
80%

What's inside TensorFlow Quantum

  1. Overview of TensorFlow Quantum (TFQ)

    master

    TensorFlow Quantum (TFQ) is a high-performance Python framework designed for hybrid quantum-classical machine learning, specifically focused on modeling quantum data. It allows developers to interleave quantum algorithms defined in Cirq with the machine learning capabilities of TensorFlow and Keras.

    Key capabilities include:

    • Quantum Circuit Integration: Uses Cirq for circuit definitions and qsim for simulations.
    • Automatic Differentiation: Provides an extensible system for computing gradients of quantum circuits using methods like parameter shift and adjoint.
    • TensorFlow Integration: Quantum operations are implemented as C++ TensorFlow Ops, allowing them to be treated as first-class citizens within the TensorFlow compute graph.
    • High-Level Abstractions: Leverages Keras to provide high-level constructs for quantum machine learning.
  2. What is TensorFlow Quantum

    master
    TensorFlow Quantum (TFQ) is a Python framework designed for quantum machine learning. It allows researchers to build hybrid quantum-classical models by interleaving quantum algorithms and logic defined in Cirq with TensorFlow. The framework focuses on handling quantum data and integrating quantum computing frameworks directly within the TensorFlow ecosystem.
  3. What is Quantum Machine Learning (QML)?

    master

    Quantum Machine Learning (QML) in the context of TensorFlow Quantum is built upon two primary pillars: quantum data and hybrid quantum-classical models.

    1. Quantum Data: Data originating from natural or artificial quantum systems (e.g., samples from a Sycamore processor). This data exhibits superposition and entanglement, often requiring exponential classical resources to represent.
    2. Hybrid Quantum-Classical Models: Because current Noisy Intermediate-Scale Quantum (NISQ) processors are small and noisy, they cannot generalize quantum data in isolation. Instead, they work in concert with classical co-processors. TensorFlow Quantum uses TensorFlow as the base platform to facilitate this heterogeneous computing (CPUs, GPUs, TPUs) to experiment with hybrid algorithms.
  4. What are Hybrid Quantum-Classical Models and QNNs?

    master

    In the NISQ (Noisy Intermediate-Scale Quantum) era, effective computation requires combining quantum processors with classical co-processors.

    A Quantum Neural Network (QNN) is a parameterized quantum computational model designed to be executed on a quantum computer. In literature and within TFQ, the term QNN is often used interchangeably with Parameterized Quantum Circuit (PQC).

  5. TensorFlow Quantum datatype primitives: Quantum circuit and Pauli sum

    master

    TFQ introduces two primary datatype primitives to integrate quantum computing with TensorFlow:

    • Quantum circuit: Represents cirq.Circuit objects within TensorFlow. These can be batched, allowing for batches of circuits with varying sizes, similar to batches of real-valued data.
    • Pauli sum: Represents linear combinations of tensor products of Pauli operators defined in Cirq (cirq.PauliSum). These can also be created in batches of varying sizes.
  6. Core data primitives in TensorFlow Quantum

    master

    TensorFlow Quantum introduces two primary datatype primitives to represent quantum information within TensorFlow:

    • Quantum circuit: Represents a Cirq-defined quantum circuit. These can be created in batches of varying sizes, similar to how standard TensorFlow handles batches of real-valued data points.
    • Pauli sum: Represents linear combinations of tensor products of Pauli operators defined in Cirq. Like circuits, these can be created in batches of varying sizes.
  7. Available operations in TensorFlow Quantum

    master

    Using the quantum circuit and Pauli sum primitives, TFQ provides several key operations for hybrid modeling:

    • Sampling: Sample from the output distributions of batches of circuits.
    • Expectation Value Calculation: Calculate the expectation value of batches of Pauli sums on batches of circuits. TFQ supports backpropagation-compatible gradient calculation for these operations.
    • Simulation: Simulate batches of circuits and states. State simulation allows researchers to inspect quantum state amplitudes with near-exact precision, which is useful for understanding how a circuit maps states.
  8. How TensorFlow Quantum integrates with Cirq

    master
    TensorFlow Quantum (TFQ) uses Cirq primitives to extend TensorFlow for batch computation, model building, and gradient computation. Cirq provides the fundamental quantum operations—such as qubits, gates, circuits, and measurement—while TFQ brings these into the TensorFlow ecosystem as tensors. To use TFQ effectively, users should be proficient with Cirq's ability to create, modify, and invoke quantum circuits.
  9. Understanding Quantum Data and its applications

    master

    Quantum data is data that exhibits quantum mechanical properties like superposition and entanglement. TensorFlow Quantum (TFQ) provides primitives to develop models that can disentangle and generalize correlations within this noisy, entangled data.

    Common use cases for processing quantum data include:

    • Chemical simulation: Extracting information about chemical structures and dynamics.
    • Quantum matter simulation: Modeling exotic states of matter with many-body quantum effects.
    • Quantum control: Using hybrid models for optimal control, calibration, and error mitigation (detection and correction).
    • Quantum communication networks: Discriminating among non-orthogonal quantum states for repeaters and receivers.
    • Quantum metrology: Designing variational quantum models for high-precision sensing and imaging.
  10. How differentiators work in TensorFlow Quantum

    master

    Because quantum observables do not have simple analytical gradient formulas (as they are often obtained via sampling), TFQ provides the tfq.differentiators module to handle gradient computation.

    Using Differentiators

    You can apply differentiation techniques in two ways:

    1. Attach to an existing op: Use generate_differentiable_op with an instance of tfq.differentiators.Differentiator.
    2. Pass to Keras layers: Provide a differentiator to the constructor of tfq.layers.Expectation or tfq.layers.SampledExpectation.

    Implementing Custom Differentiators

    • Inheritance: To create a custom differentiation method, inherit from the tfq.differentiators.Differentiator class.
    • Custom Gradients: To define a gradient operation specifically for sampling or state vector calculation, use tf.custom_gradient.

    Available Techniques

    • Analytic/Exact environment: Finite difference methods are often the fastest in terms of wall-clock time.
    • Practical/Real-world settings: Methods like parameter shift or stochastic methods are more effective for hardware-based or sample-based expectation calculations.
  11. Run code formatting with format_all.sh

    master
    Use the ./scripts/format_all.sh script to automatically apply clang-format to C++ source files and yapf to Python source files across the entire repository. This ensures code adheres to the project's style guidelines.
    ./scripts/format_all.sh