Skip to content

About the input of quantum neural network in tensorflow quantum #377

Open
@SSSSSCV

Description

@SSSSSCV

I created a quantum neural network using tensorflow quantum,It's input is a tensor converted by circuit.About this input circuit,I found that if the parameters of the circuit are also specified by tensors, the quantum neural network cannot be trained.
The circuit when using normal parameters can make the network train normally

theta_g=1
blob_size = abs(1 - 4) / 5
spread_x = np.random.uniform(-blob_size, blob_size)
spread_y = np.random.uniform(-blob_size, blob_size)
angle = theta_g + spread_y
cir=cirq.Circuit(cirq.ry(-angle)(qubit), cirq.rx(-spread_x)(qubit))
discriminator_network(tfq.convert_to_tensor([cir]))

But when I use the following code, the quantum neural network cannot be trained

theta_g=tf.constant([1])
blob_size = abs(1 - 4) / 5
spread_x = np.random.uniform(-blob_size, blob_size)
spread_y = np.random.uniform(-blob_size, blob_size)
spred_x = tf.constant(spread_x)
spred_y = tf.constant(spread_y)
angle = theta_g + spread_y
cir=cirq.Circuit(cirq.ry(-angle)(qubit), cirq.rx(-spread_x)(qubit))
discriminator_network(tfq.convert_to_tensor([cir]))

The discriminator_network

def discriminator():
    theta = sympy.Symbol('theta')
    q_model = cirq.Circuit(cirq.ry(theta)(qubit))
    q_data_input = tf.keras.Input(
        shape=(), dtype=tf.dtypes.string)
    expectation = tfq.layers.PQC(q_model, cirq.Z(qubit))
    expectation_output = expectation(q_data_input)

    classifier = tf.keras.layers.Dense(1, activation=tf.keras.activations.sigmoid)
    classifier_output = classifier(expectation_output)
    model = tf.keras.Model(inputs=q_data_input, outputs=classifier_output)
    return model

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions