### Install TensorBoard Plugin Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/research_tools.ipynb Install the TensorBoard profile plugin. ```bash !pip install tensorboard_plugin_profile==2.15.0 ``` -------------------------------- ### Create and Install Python Package Source: https://github.com/tensorflow/quantum/blob/master/docs/install.md Generate the pip package and install it into the environment. ```bash bazel-bin/release/build_pip_package /tmp/tfquantum/ pip install /tmp/tfquantum/name_of_generated_wheel.whl ``` -------------------------------- ### Import Libraries and Setup Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/barren_plateaus.ipynb Imports necessary modules and initializes the random seed for reproducibility. ```python import tensorflow as tf import tensorflow_quantum as tfq import cirq import sympy import numpy as np # visualization tools %matplotlib inline import matplotlib.pyplot as plt from cirq.contrib.svg import SVGCircuit np.random.seed(1234) ``` -------------------------------- ### Install TensorFlow Docs Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/noise.ipynb Install the TensorFlow documentation package for plotting utilities. ```bash !pip install -q git+https://github.com/tensorflow/docs ``` -------------------------------- ### Visualize Data Sample Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/mnist.ipynb Display the first training example and its label. ```python print(y_train[0]) plt.imshow(x_train[0, :, :, 0]) plt.colorbar() ``` -------------------------------- ### Install Dependencies Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/barren_plateaus.ipynb Installs the required versions of TensorFlow and TensorFlow Quantum. ```bash # In Colab, you will be asked to restart the session after this finishes. !pip install tensorflow==2.18.1 tensorflow-quantum==0.7.6 ``` -------------------------------- ### Launch TensorBoard Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/research_tools.ipynb Command to start the TensorBoard server pointing to the specified log directory. ```python #docs_infra: no_execute %tensorboard --logdir tb_logs/ ``` -------------------------------- ### Install Dependencies Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/mnist.ipynb Install the required versions of TensorFlow, TensorFlow Quantum, and Seaborn. ```bash # In Colab, you will be asked to restart the session after this finishes. !pip install tensorflow==2.18.1 tensorflow-quantum==0.7.6 ``` ```bash # Install seaborn for use later in this notebook. !pip install seaborn ``` -------------------------------- ### Instantiate and Visualize Circuit Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/quantum_reinforcement_learning.ipynb Example usage to create a 3-qubit circuit with one layer and visualize it using SVGCircuit. ```python n_qubits, n_layers = 3, 1 qubits = cirq.GridQubit.rect(1, n_qubits) circuit, _, _ = generate_circuit(qubits, n_layers) SVGCircuit(circuit) ``` -------------------------------- ### Set up Python 3 development environment Source: https://github.com/tensorflow/quantum/blob/master/docs/install.md Install necessary system dependencies and Python 3.10 development tools for building from source. ```bash sudo apt update sudo apt-get install pkg-config zip g++ zlib1g-dev unzip python3.10 sudo apt install python3.10 python3.10-dev python3.10-venv python3-pip python3.10 -m pip install --upgrade pip ``` -------------------------------- ### Upgrade pip and install TensorFlow Source: https://github.com/tensorflow/quantum/blob/master/docs/install.md Prepare the environment by upgrading pip and installing the required version of TensorFlow. ```bash pip install --upgrade pip pip install tensorflow==2.18.1 ``` -------------------------------- ### Demonstrate Circuit Layer Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/mnist.ipynb Creates an example circuit layer using the CircuitLayerBuilder to visualize its structure. ```python demo_builder = CircuitLayerBuilder(data_qubits=cirq.GridQubit.rect(4, 1), readout=cirq.GridQubit(-1, -1)) circuit = cirq.Circuit() demo_builder.add_layer(circuit, gate=cirq.XX, prefix='xx') SVGCircuit(circuit) ``` -------------------------------- ### Download and install Bazel 6.5.0 Source: https://github.com/tensorflow/quantum/blob/master/docs/install.md Download and install the specific Bazel version required for building TensorFlow Quantum. ```bash wget https://github.com/bazelbuild/bazel/releases/download/6.5.0/bazel_6.5.0-linux-x86_64.deb sudo dpkg -i bazel_6.5.0-linux-x86_64.deb ``` -------------------------------- ### Verify Bazel installation Source: https://github.com/tensorflow/quantum/blob/master/docs/install.md Confirm that the correct version of Bazel is installed. ```bash bazel --version ``` -------------------------------- ### Install TensorFlow Quantum Wheel in Colab Source: https://github.com/tensorflow/quantum/blob/master/release/README.md Replace the standard pip install command in a Colab notebook with the path to a custom-built wheel file. ```bash !pip install /here/paste/the/path/to/the/wheel/file ``` -------------------------------- ### Install TensorFlow Quantum via pip Source: https://github.com/tensorflow/quantum/blob/master/docs/install.md Install the latest stable release of the TensorFlow Quantum package. ```bash pip install -U tensorflow-quantum ``` -------------------------------- ### Install TensorFlow Quantum Dependencies Source: https://github.com/tensorflow/quantum/blob/master/docs/install.md Install required dependencies for TensorFlow Quantum. ```bash pip install -r requirements.txt ``` -------------------------------- ### Controller Output Example Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/hello_many_worlds.ipynb Demonstrates the output of the classical controller network for a batch of inputs. The output values are control signals that will be fed into a quantum circuit. ```python controller(tf.constant([[0.0], [1.0]])).numpy() ``` -------------------------------- ### Clone and Prepare TensorFlow Quantum Source: https://github.com/tensorflow/quantum/blob/master/docs/install.md Download the forked TensorFlow Quantum repository and install dependencies. ```bash git clone https://github.com/username/quantum.git cd quantum pip install -r requirements.txt ``` -------------------------------- ### Setup TensorBoard Filewriter Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/research_tools.ipynb Initializes a TensorBoard file writer to log metrics during training. This involves creating a log directory based on the current timestamp and setting the writer as the default for summary operations. ```python logdir = "tb_logs/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S") file_writer = tf.summary.create_file_writer(logdir + "/metrics") file_writer.set_as_default() ``` -------------------------------- ### Benchmark a Clifford circuit Source: https://github.com/tensorflow/quantum/blob/master/benchmarks/README.md Example command to benchmark a dense depth-10 Clifford circuit over 5 qubits. ```bash bazel run -c opt --cxxopt="-D_GLIBCXX_USE_CXX11_ABI=1" --cxxopt="-msse2" \ --cxxopt="-msse3" --cxxopt="-msse4" \ benchmarks/scripts:benchmark_clifford_circuit -- \ --n_moments 5 --n_qubits 4 \ --benchmarks=benchmark_clifford_circuit_eager ``` -------------------------------- ### Benchmark parameter shift differentiation Source: https://github.com/tensorflow/quantum/blob/master/benchmarks/README.md Example command to benchmark the parameter shift method on a random 4-qubit circuit. ```bash bazel run -c opt --cxxopt="-D_GLIBCXX_USE_CXX11_ABI=1" --cxxopt="-msse2" \ --cxxopt="-msse3" --cxxopt="-msse4" \ benchmarks/scripts:benchmark_op_gradients -- \ --n_moments 10 --n_qubits 4 --n_symbols 10 \ --n_runs 50 --batch_size 10 \ --benchmarks=benchmark_parameter_shift ``` -------------------------------- ### Log Training Statistics Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/research_tools.ipynb Example of logging histogram data and tracking epoch timing within a training loop. ```python random_new_distribution = generate_data(REFERENCE_CIRCUIT, N_SAMPLES) tf.summary.histogram('New round of True samples', data=bits_to_ints(random_new_distribution), step=epoch) if epoch % 10 == 0: print(f'Epoch {epoch}, took {time.time() - t}(s)') t = time.time() ``` -------------------------------- ### Set Training Parameters for QNN Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/mnist.ipynb Defines the number of epochs, batch size, and the number of training examples to use for the QNN. Adjust NUM_EXAMPLES to reduce training time. ```python EPOCHS = 3 BATCH_SIZE = 32 NUM_EXAMPLES = len(x_train_tfcirc) ``` -------------------------------- ### Profile Model Performance Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/research_tools.ipynb Wraps the training function with profiler start and stop calls to capture performance metrics. ```python tf.profiler.experimental.start(logdir) train(all_data, epochs=10, start_epoch=50) tf.profiler.experimental.stop() ``` -------------------------------- ### Initialize Data Encoding Circuits Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/quantum_data.ipynb Sets up the qubits and prepares the training and testing circuits using the PQK preparation function. ```python qubits = cirq.GridQubit.rect(1, DATASET_DIM + 1) q_x_train_circuits = prepare_pqk_circuits(qubits, x_train) q_x_test_circuits = prepare_pqk_circuits(qubits, x_test) ``` -------------------------------- ### Create and activate virtual environment Source: https://github.com/tensorflow/quantum/blob/master/docs/install.md Initialize a virtual environment for isolated development. ```bash python3.10 -m venv quantum_env source quantum_env/bin/activate ``` -------------------------------- ### Run a benchmark with parameters Source: https://github.com/tensorflow/quantum/blob/master/benchmarks/README.md Execute a benchmark with custom parameters and specific method names, separated from Bazel flags by a double dash. ```bash bazel run benchmarks/scripts: -- --benchmarks= ``` -------------------------------- ### Exit TensorFlow Directory Source: https://github.com/tensorflow/quantum/blob/master/docs/install.md Navigate out of the TensorFlow directory after installation. ```bash cd .. ``` -------------------------------- ### Initialize and Synchronize Q-learning Models Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/quantum_reinforcement_learning.ipynb Creates the main Q-function model and its target counterpart. The target model's weights are initialized to match the main model's weights to ensure stable learning. ```python model = generate_model_Qlearning(qubits, n_layers, n_actions, observables, False) model_target = generate_model_Qlearning(qubits, n_layers, n_actions, observables, True) model_target.set_weights(model.get_weights()) ``` -------------------------------- ### Verify dataset size Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/quantum_data.ipynb Prints the updated number of training and testing examples after truncation. ```python print("New number of training examples:", len(x_train)) print("New number of test examples:", len(x_test)) ``` -------------------------------- ### Configure TensorFlow Quantum Build Source: https://github.com/tensorflow/quantum/blob/master/docs/install.md Run the configuration script to prepare the build environment. ```bash ./configure.sh ``` -------------------------------- ### Initialize Qubits and Observables for PQC Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/quantum_reinforcement_learning.ipynb Sets up the qubits and defines the global Pauli product observable (Z_0*Z_1*Z_2*Z_3) used in the PQC policy. ```python n_qubits = 4 # Dimension of the state vectors in CartPole n_layers = 5 # Number of layers in the PQC n_actions = 2 # Number of actions in CartPole qubits = cirq.GridQubit.rect(1, n_qubits) ``` ```python ops = [cirq.Z(q) for q in qubits] observables = [reduce((lambda x, y: x * y), ops)] # Z_0*Z_1*Z_2*Z_3 ``` -------------------------------- ### Load Built-in Quantum Datasets Source: https://context7.com/tensorflow/quantum/llms.txt Demonstrates loading pre-built quantum datasets like TFI chains, XXZ chains, and cluster states for benchmarking. ```python import tensorflow_quantum as tfq import matplotlib.pyplot as plt # Load transverse-field Ising model data qubits = cirq.GridQubit.rect(1, 4) circuits, labels, pauli_sums, addinfo = tfq.datasets.tfi_chain( qubits, data_dir=None # Use default ) print(f"Number of circuits: {len(circuits)}") print(f"Label shape: {labels.shape}") print(f"Sample circuit:\n{circuits[0]}") # Load XXZ chain data qubits = cirq.GridQubit.rect(1, 4) circuits, labels, pauli_sums, addinfo = tfq.datasets.xxz_chain( qubits, data_dir=None ) print(f"\nXXZ chain circuits: {len(circuits)}") print(f"Labels range: [{labels.min():.2f}, {labels.max():.2f}]") # Load excited cluster states qubits = cirq.GridQubit.rect(1, 6) circuits, labels = tfq.datasets.excited_cluster_states(qubits) print(f"\nCluster state circuits: {len(circuits)}") print(f"Unique labels: {set(labels.flatten().tolist())}") ``` -------------------------------- ### Run a benchmark testcase Source: https://github.com/tensorflow/quantum/blob/master/benchmarks/README.md Execute a specific benchmark file using Bazel. ```bash bazel run benchmarks/scripts: ``` -------------------------------- ### Configure Reinforcement Learning Hyperparameters Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/quantum_reinforcement_learning.ipynb Sets up replay memory and epsilon-greedy parameters for agent training. ```python gamma = 0.99 n_episodes = 2000 # Define replay memory max_memory_length = 10000 # Maximum replay length replay_memory = deque(maxlen=max_memory_length) epsilon = 1.0 # Epsilon greedy parameter epsilon_min = 0.01 # Minimum epsilon greedy parameter decay_epsilon = 0.99 # Decay rate of epsilon greedy parameter batch_size = 16 steps_per_update = 10 # Train the model every x steps steps_per_target_update = 30 # Update the target model every x steps ``` -------------------------------- ### Remove Contradictory Examples Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/mnist.ipynb Filters a dataset to remove images that possess multiple conflicting labels. Requires collections and numpy. ```python def remove_contradicting(xs, ys): mapping = collections.defaultdict(set) orig_x = {} # Determine the set of labels for each unique image: for x, y in zip(xs, ys): orig_x[tuple(x.flatten())] = x mapping[tuple(x.flatten())].add(y) new_x = [] new_y = [] for flatten_x in mapping: x = orig_x[flatten_x] labels = mapping[flatten_x] if len(labels) == 1: new_x.append(x) new_y.append(next(iter(labels))) else: # Throw out images that match more than one label. pass num_uniq_3 = sum( 1 for value in mapping.values() if len(value) == 1 and True in value) num_uniq_6 = sum( 1 for value in mapping.values() if len(value) == 1 and False in value) num_uniq_both = sum(1 for value in mapping.values() if len(value) == 2) print("Number of unique images:", len(mapping.values())) print("Number of unique 3s: ", num_uniq_3) print("Number of unique 6s: ", num_uniq_6) print("Number of unique contradicting labels (both 3 and 6): ", num_uniq_both) print() print("Initial number of images: ", len(xs)) print("Remaining non-contradicting unique images: ", len(new_x)) return np.array(new_x), np.array(new_y) ``` ```python x_train_nocon, y_train_nocon = remove_contradicting(x_train_small, y_train) ``` -------------------------------- ### Initialize PQC Observables for CartPole Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/quantum_reinforcement_learning.ipynb Sets up the qubits and defines the specific Pauli products to be used as observables for different actions in the CartPole environment. These observables are crucial for the PQC's Q-function approximation. ```python n_qubits = 4 # Dimension of the state vectors in CartPole n_layers = 5 # Number of layers in the PQC n_actions = 2 # Number of actions in CartPole qubits = cirq.GridQubit.rect(1, n_qubits) ops = [cirq.Z(q) for q in qubits] observables = [ops[0] * ops[1], ops[2] * ops[3]] # Z_0*Z_1 for action 0 and Z_2*Z_3 for action 1 ``` -------------------------------- ### Plot Keras Model Architecture Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/hello_many_worlds.ipynb Generates a visual representation of the Keras model, including input/output shapes. Requires the `graphviz` package to be installed. ```python tf.keras.utils.plot_model(model, show_shapes=True, dpi=70) ``` -------------------------------- ### Display Sample Data Points Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/qcnn.ipynb Prints sample input quantum circuits and their corresponding labels to visualize the generated data. ```python sample_points, sample_labels, _, __ = generate_data(cirq.GridQubit.rect(1, 4)) print('Input:', tfq.from_tensor(sample_points)[0], 'Output:', sample_labels[0]) print('Input:', tfq.from_tensor(sample_points)[1], 'Output:', sample_labels[1]) ``` -------------------------------- ### Get Spectrum (Eigenvalues/Eigenvectors) in Python Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/quantum_data.ipynb Computes the eigenvalues and eigenvectors of the kernel matrix for a given set of data points. Requires the `compute_kernel_matrix` function. ```python def get_spectrum(datapoints, gamma=1.0): """Compute the eigenvalues and eigenvectors of the kernel of datapoints.""" KC_qs = compute_kernel_matrix(datapoints, gamma) S, V = tf.linalg.eigh(KC_qs) S = tf.math.abs(S) return S, V ``` -------------------------------- ### Import Libraries Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/mnist.ipynb Import necessary modules for quantum computing, data processing, and visualization. ```python import tensorflow as tf import tensorflow_quantum as tfq import cirq import sympy import numpy as np import seaborn as sns import collections # visualization tools %matplotlib inline import matplotlib.pyplot as plt from cirq.contrib.svg import SVGCircuit ``` -------------------------------- ### Inspect Circuit and Samples Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/research_tools.ipynb Visualize the circuit structure and print raw bitstring samples. ```python qubits = cirq.GridQubit.rect(1, 5) random_circuit_m = generate_circuit(qubits) + cirq.measure_each(*qubits) SVGCircuit(random_circuit_m) ``` ```python samples = cirq.sample(random_circuit_m, repetitions=10) print('10 Random bitstrings from this circuit:') print(samples) ``` -------------------------------- ### Get Final Model Outputs After Training Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/hello_many_worlds.ipynb Retrieves the output predictions from the trained TensorFlow Quantum model using the input circuits and commands. This provides the final corrected expectation values. ```python model([datapoint_circuits, commands]) ``` -------------------------------- ### Import Dependencies Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/quantum_data.ipynb Import necessary libraries for quantum circuit construction, data manipulation, and visualization. ```python import cirq import sympy import numpy as np import tensorflow as tf import tensorflow_quantum as tfq # visualization tools %matplotlib inline import matplotlib.pyplot as plt from cirq.contrib.svg import SVGCircuit np.random.seed(1234) ``` -------------------------------- ### Run TensorFlow Quantum in Docker Container Source: https://github.com/tensorflow/quantum/blob/master/release/docker/README.md Execute a Docker container with your TensorFlow Quantum source code mounted, providing an isolated environment for testing. This example uses Ubuntu 24.04 with Python 3.12. ```shell cd $(git rev-parse --show-toplevel) docker run -it --rm --network host -v .:/tfq ubuntu24-cp312 ``` -------------------------------- ### Initialize Optimizers for PQC Training Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/quantum_reinforcement_learning.ipynb Sets up Adam optimizers with specified learning rates for training the PQC model. Separate optimizers are used for different parts of the model (e.g., input vs. variational parameters). ```python optimizer_in = tf.keras.optimizers.Adam(learning_rate=0.1, amsgrad=True) optimizer_var = tf.keras.optimizers.Adam(learning_rate=0.01, amsgrad=True) ``` -------------------------------- ### Configure and train a noiseless quantum model Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/noise.ipynb Sets up the noiseless model architecture, compiles it with an Adam optimizer, and executes the training process. ```python training_histories = dict() depolarize_p = 0. n_epochs = 50 phase_classifier = build_keras_model(qubits, depolarize_p) phase_classifier.compile( optimizer=tf.keras.optimizers.Adam(learning_rate=0.02), loss=tf.keras.losses.BinaryCrossentropy(from_logits=True), metrics=['accuracy']) # Show the keras plot of the model tf.keras.utils.plot_model(phase_classifier, show_shapes=True, dpi=70) ``` ```python noiseless_data, noiseless_labels = get_data(qubits, depolarize_p) training_histories['noiseless'] = phase_classifier.fit(x=noiseless_data, y=noiseless_labels, batch_size=16, epochs=n_epochs, validation_split=0.15, verbose=1) ``` -------------------------------- ### Format All Source Files Source: https://github.com/tensorflow/quantum/blob/master/scripts/README.md Applies clang-format and yapf to all source files for consistent code formatting. Run this before submitting a PR. ```bash ./scripts/format_all.sh ``` -------------------------------- ### Interact with Environment in Q-learning Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/quantum_reinforcement_learning.ipynb Performs a single step of interaction with the environment. It preprocesses the state, samples an action based on the current policy (epsilon-greedy strategy), and records the resulting state, reward, and done status. ```python def interact_env(state, model, epsilon, n_actions, env): # Preprocess state state_array = np.array(state) state = tf.convert_to_tensor([state_array]) # Sample action coin = np.random.random() if coin > epsilon: q_vals = model([state]) action = int(tf.argmax(q_vals[0]).numpy()) else: action = np.random.choice(n_actions) # Apply sampled action in the environment, receive reward and next state next_state, reward, terminated, truncated, _ = env.step(action) done = terminated or truncated interaction = { 'state': state_array, 'action': action, 'next_state': next_state.copy(), 'reward': reward, 'done': np.float32(done) } return interaction ``` -------------------------------- ### Configure and train a noisy quantum model Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/noise.ipynb Initializes a model with a non-zero depolarization probability and trains it, noting that noisy simulation is computationally more expensive. ```python depolarize_p = 0.001 n_epochs = 50 noisy_phase_classifier = build_keras_model(qubits, depolarize_p) noisy_phase_classifier.compile( optimizer=tf.keras.optimizers.Adam(learning_rate=0.02), loss=tf.keras.losses.BinaryCrossentropy(from_logits=True), metrics=['accuracy']) # Show the keras plot of the model tf.keras.utils.plot_model(noisy_phase_classifier, show_shapes=True, dpi=70) ``` ```python noisy_data, noisy_labels = get_data(qubits, depolarize_p) training_histories['noisy'] = noisy_phase_classifier.fit(x=noisy_data, y=noisy_labels, batch_size=16, epochs=n_epochs, validation_split=0.15, verbose=1) ``` -------------------------------- ### Import Required Modules Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/noise.ipynb Import necessary libraries for quantum circuit simulation, data handling, and plotting. ```python import random import cirq import sympy import tensorflow_quantum as tfq import tensorflow as tf import numpy as np # Plotting import matplotlib.pyplot as plt import tensorflow_docs as tfdocs import tensorflow_docs.plots ``` -------------------------------- ### Initialize Model Parameters Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/quantum_reinforcement_learning.ipynb Assigns initial values to the input, variable, and output weights. ```python w_in, w_var, w_out = 1, 0, 2 ``` -------------------------------- ### Calculate Gradients with tf.GradientTape Source: https://context7.com/tensorflow/quantum/llms.txt Demonstrates how to calculate gradients of quantum circuit expectation values with respect to circuit parameters using tf.GradientTape. ```python symbol_values_tf = tf.Variable(symbol_values) with tf.GradientTape() as tape: expectations = expectation_layer( circuit, symbol_names=symbol_names, symbol_values=symbol_values_tf, operators=cirq.Z(qubit) ) grads = tape.gradient(expectations, symbol_values_tf) print("Gradients of Z expectation w.r.t. parameters:") print(grads.numpy()) ``` -------------------------------- ### Initialize Environment for Training Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/quantum_reinforcement_learning.ipynb This snippet initializes the environment for the agent's training loop. The training process can be computationally intensive and may take a significant amount of time to complete. ```python env_name = "CartPole-v1" ``` -------------------------------- ### Instantiate Quantum Model Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/mnist.ipynb Calls the create_quantum_model function to obtain the model circuit and readout operation. ```python model_circuit, model_readout = create_quantum_model() ``` -------------------------------- ### Build TensorFlow Quantum Source: https://github.com/tensorflow/quantum/blob/master/docs/install.md Compile TensorFlow Quantum using Bazel with optimized flags. ```bash bazel build -c opt --cxxopt="-O3" --cxxopt="-march=native" release:build_pip_package ``` -------------------------------- ### Compile and Train the Hybrid Model Source: https://context7.com/tensorflow/quantum/llms.txt Configures the model with an optimizer and loss function, then trains it on synthetic data. ```python model = HybridQNN() model.compile( optimizer=tf.keras.optimizers.Adam(learning_rate=0.01), loss='binary_crossentropy', metrics=['accuracy'] ) # Generate synthetic data np.random.seed(42) X_train = np.random.randn(100, 2).astype(np.float32) y_train = (X_train[:, 0] * X_train[:, 1] > 0).astype(np.float32).reshape(-1, 1) # Train history = model.fit(X_train, y_train, epochs=20, batch_size=16, verbose=0) print(f"Final accuracy: {history.history['accuracy'][-1]:.2%}") print(f"Final loss: {history.history['loss'][-1]:.4f}") ``` -------------------------------- ### Load quantum dataset circuits Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/noise.ipynb Retrieve circuits and labels from the tfq.datasets module for training. ```python qubits = cirq.GridQubit.rect(1, 8) circuits, labels, pauli_sums, _ = tfq.datasets.xxz_chain(qubits, 'closed') circuits[0] ``` -------------------------------- ### Prepare PQK Circuits Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/quantum_data.ipynb Assembles the full encoding circuits by combining the rotation wall and the parameterized V-theta circuit. ```python def prepare_pqk_circuits(qubits, classical_source, n_trotter=10): """Prepare the pqk feature circuits around a dataset.""" n_qubits = len(qubits) n_points = len(classical_source) # Prepare random single qubit rotation wall. random_rots = np.random.uniform(-2, 2, size=(n_qubits, 3)) initial_U = single_qubit_wall(qubits, random_rots) # Prepare parametrized V V_circuit, symbols = v_theta(qubits) exp_circuit = cirq.Circuit(V_circuit for t in range(n_trotter)) # Convert to `tf.Tensor` initial_U_tensor = tfq.convert_to_tensor([initial_U]) initial_U_splat = tf.tile(initial_U_tensor, [n_points]) full_circuits = tfq.layers.AddCircuit()(initial_U_splat, append=exp_circuit) # Replace placeholders in circuits with values from `classical_source`. return tfq.resolve_parameters( full_circuits, tf.convert_to_tensor([str(x) for x in symbols]), tf.convert_to_tensor(classical_source * (n_qubits / 3) / n_trotter)) ``` -------------------------------- ### Prepare dataset for quantum control training Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/hello_many_worlds.ipynb Define the operator data, command inputs, and expected outputs for the training process. ```python # The operators to measure, for each command. operator_data = tfq.convert_to_tensor([[cirq.X(qubit)], [cirq.Z(qubit)]]) # The command input values to the classical NN. commands = np.array([[0], [1]], dtype=np.float32) # The desired expectation value at output of quantum circuit. expected_outputs = np.array([[1], [-1]], dtype=np.float32) ``` -------------------------------- ### Visualize QCNN Unitary and Pooling Circuits Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/qcnn.ipynb Displays the generated circuits using SVGCircuit. ```python SVGCircuit(one_qubit_unitary(cirq.GridQubit(0, 0), sympy.symbols('x0:3'))) ``` ```python SVGCircuit(two_qubit_unitary(cirq.GridQubit.rect(1, 2), sympy.symbols('x0:15'))) ``` ```python SVGCircuit(two_qubit_pool(*cirq.GridQubit.rect(1, 2), sympy.symbols('x0:6'))) ``` -------------------------------- ### Execute Training Loop Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/research_tools.ipynb Initiates the training process for a specified number of epochs. ```python train(all_data, epochs=50) ``` -------------------------------- ### Set Environment and Training Parameters Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/quantum_reinforcement_learning.ipynb Defines key parameters for the reinforcement learning process, including state bounds, discount factor, batch size, and number of episodes. ```python state_bounds = np.array([2.4, 2.5, 0.21, 2.5]) gamma = 1 batch_size = 10 n_episodes = 1000 ``` -------------------------------- ### Define Training Data for Qubit Control Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/hello_many_worlds.ipynb Sets up the classical command inputs and the desired quantum state expectation values for training. `commands` are binary inputs for the controller, and `expected_outputs` represent the target Z expectation values. ```python # The command input values to the classical NN. commands = np.array([[0], [1]], dtype=np.float32) # The desired Z expectation value at output of quantum circuit. expected_outputs = np.array([[1], [-1]], dtype=np.float32) ``` -------------------------------- ### Locate benchmark report Source: https://github.com/tensorflow/quantum/blob/master/benchmarks/README.md Path format for the generated proto benchmark report. ```text benchmarks/scripts/reports/CliffordBenchmarks.benchmark_clifford_circuit_4_5_1 ``` -------------------------------- ### Import Libraries Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/hello_many_worlds.ipynb Import core libraries including TensorFlow, TensorFlow Quantum, Cirq, and visualization tools. ```python import tensorflow as tf import tensorflow_quantum as tfq import cirq import sympy import numpy as np # visualization tools %matplotlib inline import matplotlib.pyplot as plt from cirq.contrib.svg import SVGCircuit ``` -------------------------------- ### License Header Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/barren_plateaus.ipynb Apache License 2.0 header for the source file. ```python #@title Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. ``` -------------------------------- ### Compile and train hybrid model Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/qcnn.ipynb Configures the optimizer and loss function, then executes the training loop on provided excitation data. ```python hybrid_model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.02), loss=tf.losses.mse, metrics=[custom_accuracy]) hybrid_history = hybrid_model.fit(x=train_excitations, y=train_labels, batch_size=16, epochs=25, verbose=1, validation_data=(test_excitations, test_labels)) ``` -------------------------------- ### Initialize Adam Optimizers Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/quantum_reinforcement_learning.ipynb Configures separate Adam optimizers for different layers of the quantum model. ```python optimizer_in = tf.keras.optimizers.Adam(learning_rate=0.001, amsgrad=True) optimizer_var = tf.keras.optimizers.Adam(learning_rate=0.001, amsgrad=True) optimizer_out = tf.keras.optimizers.Adam(learning_rate=0.1, amsgrad=True) ``` -------------------------------- ### Test Run the TensorFlow Quantum Model Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/hello_many_worlds.ipynb Performs an initial test run of the `tfq` model with the defined input circuits and commands to observe its output before training. ```python model([datapoint_circuits, commands]).numpy() ``` -------------------------------- ### Visualize Agent Performance in Cartpole Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/quantum_reinforcement_learning.ipynb This code snippet demonstrates how to visualize the performance of the trained agent in the Cartpole environment by rendering a sample episode and saving it as a GIF. Requires a display environment. ```python # from PIL import Image # env = gym.make('CartPole-v1', render_mode="rgb_array") # state, _ = env.reset() # frames = [] # for t in range(500): # im = Image.fromarray(env.render()) # frames.append(im) # policy = model([tf.convert_to_tensor([state/state_bounds])]) # action = np.random.choice(n_actions, p=policy.numpy()[0]) # state, _, terminated, truncated, _ = env.step(action) # done = terminated or truncated # if done: # break # env.close() # frames[1].save('./images/gym_CartPole.gif', # save_all=True, append_images=frames[2:], optimize=False, duration=40, loop=0) ``` -------------------------------- ### Prepare Stilted Dataset Labels in Python Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/quantum_data.ipynb Prepares new labels for the dataset to maximize the geometric distance between the original and PQK kernel matrices. This function requires eigenvalues and eigenvectors from both kernel matrices and a lambda value. ```python def get_stilted_dataset(S, V, S_2, V_2, lambdav=1.1): """Prepare new labels that maximize geometric distance between kernels.""" S_diag = tf.linalg.diag(S**0.5) S_2_diag = tf.linalg.diag(S_2 / (S_2 + lambdav)**2) scaling = S_diag @ tf.transpose(V) @ \ V_2 @ S_2_diag @ tf.transpose(V_2) @ \ V @ S_diag # Generate new lables using the largest eigenvector. _, vecs = tf.linalg.eig(scaling) new_labels = tf.math.real( tf.einsum('ij,j->i', tf.cast(V @ S_diag, tf.complex64), vecs[-1])).numpy() # Create new labels and add some small amount of noise. final_y = new_labels > np.median(new_labels) noisy_y = (final_y ^ (np.random.uniform(size=final_y.shape) > 0.95)) return noisy_y ``` -------------------------------- ### Load and Preprocess Fashion-MNIST Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/quantum_data.ipynb Load the Fashion-MNIST dataset and rescale pixel values to the [0.0, 1.0] range. ```python (x_train, y_train), (x_test, y_test) = tf.keras.datasets.fashion_mnist.load_data() # Rescale the images from [0,255] to the [0.0,1.0] range. x_train, x_test = x_train / 255.0, x_test / 255.0 print("Number of original training examples:", len(x_train)) print("Number of original test examples:", len(x_test)) ``` -------------------------------- ### Import Required Modules Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/research_tools.ipynb Import TensorFlow, TensorFlow Quantum, Cirq, and visualization libraries. ```python import datetime import time import cirq import tensorflow as tf import tensorflow_quantum as tfq from tensorflow.keras import layers # visualization tools %matplotlib inline import matplotlib.pyplot as plt from cirq.contrib.svg import SVGCircuit ``` -------------------------------- ### Verify Single Qubit Wall Circuit Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/quantum_data.ipynb Visualizes the generated single-qubit wall circuit using SVGCircuit. ```python SVGCircuit( single_qubit_wall(cirq.GridQubit.rect(1, 4), np.random.uniform(size=(4, 3)))) ``` -------------------------------- ### Load TensorBoard Extension Source: https://github.com/tensorflow/quantum/blob/master/docs/tutorials/research_tools.ipynb Load the TensorBoard magic extension for Jupyter notebooks. ```python #docs_infra: no_execute %load_ext tensorboard ``` -------------------------------- ### Create Docker Images Source: https://github.com/tensorflow/quantum/blob/master/release/docker/README.md Run the create_docker_images.sh script to build Docker images with various Python versions on Ubuntu 22.04 and 24.04. ```shell ./create_docker_images.sh ```