tensorflow/quantum

Can you encode data with fewer dimensions than the number of qubits used?

Shuhul24 opened this issue · 4 comments

Is it possible to encode data with less dimension than the number of qubits used? I am building a circuit in which I am using a circuit with qubits [cirq.GridQubit(1, 0), cirq.GridQubit(1, 1), cirq.GridQubit(1, 2)] but I have got encoded data of dimension 2, hence just wanted to encode data on qubits cirq.GridQubit(1, 0), cirq.GridQubit(1, 1) and use the rest of the qubtis for the variational ansatz. Is it possible to do so?

Sure, if you have a ControlledPQC, just have the first input weights be the however many inputs you want (assuming you are doing some sort of angle encoding)

Can you mention a small piece of sample code?

import tensorflow as tf
import tensorflow_quantum as tfq 
import numpy as np
import cirq
import sympy

data = tf.random.uniform(minval=-1, maxval=1, shape=(100, 2)) # random 2D data

qubits = [cirq.GridQubit(1, 0), cirq.GridQubit(1, 1), cirq.GridQubit(1, 2)]
params = sympy.symbols("q0:7")

def encoder():
  c = cirq.Circuit()
  c += cirq.ry(params[0]).on(qubits[0])
  c += cirq.ry(params[1]).on(qubits[1])
  return c

def vqc():
  c = cirq.Circuit()
  c += cirq.rz(params[2]).on(qubits[0])
  c += cirq.rz(params[3]).on(qubits[1])
  c += cirq.rz(params[4]).on(qubits[2])
  c += cirq.CNOT(qubits[0], qubits[1]) ** params[5]
  c += cirq.CNOT(qubits[1], qubits[2]) ** params[6]
  return c

full_circuit = encoder() + vqc()
print(full_circuit)

cpqc = tfq.layers.ControlledPQC(full_circuit, [cirq.Z(i) for i in qubits])

empty_circuit = tfq.convert_to_tensor([cirq.Circuit()])
circuit_params = tf.Variable(initial_value=np.ones(shape=(100, 5)), dtype="float32", trainable=True)

cpqc([tf.tile(empty_circuit, [100]), tf.concat((data, circuit_params), axis=-1)])