import numpy as np
import os
import tensorflow as tf
import strawberryfields as sf
from strawberryfields.ops import Dgate, BSgate, Kgate, Sgate, Rgate
import sys
sys.path.append("..")
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
# ===================================================================================
# Hyperparameters
# ===================================================================================
# Two modes required: one for "genuine" transactions and one for "fradulent"
mode_number = 2
# Number of photonic quantum layers
depth = 4
# Fock basis truncation
cutoff = 10
# Number of batches in optimization
reps = 30000
# Label for simulation
simulation_label = 1
# Number of batches to use in the optimization
batch_size = 24
# Random initialization of gate parameters
sdev_photon = 0.1
sdev = 1
# Variable clipping values
disp_clip = 5
sq_clip = 5
kerr_clip = 1
# If loading from checkpoint, previous batch number reached
ckpt_val = 0
# Number of repetitions between each output to TensorBoard
tb_reps = 100
# Number of repetitions between each model save
savr_reps = 1000
model_string = str(simulation_label)
# Target location of output
folder_locator = './outputs/'
# Locations of TensorBoard and model save outputs
board_string = folder_locator + 'tensorboard/' + model_string + '/'
checkpoint_string = folder_locator + 'models/' + model_string + '/'
# ===================================================================================
# Loading the training data
# ===================================================================================
# Data outputted from data_processor.py
data_genuine = np.loadtxt('creditcard_genuine_1.csv', delimiter=',')
data_fraudulent = np.loadtxt('creditcard_fraudulent_1.csv', delimiter=',')
# Combining genuine and fraudulent data
data_combined = np.append(data_genuine, data_fraudulent, axis=0)
data_points = len(data_combined)
# ===================================================================================
# Setting up the classical NN input
# ===================================================================================
# Input neurons
input_neurons = 10
# Widths of hidden layers
nn_architecture = [10, 10]
# Output neurons of classical part
output_neurons = 14
# Defining classical network parameters
input_classical_layer = tf.placeholder(tf.float32, shape=[batch_size, input_neurons])
layer_matrix_1 = tf.Variable(tf.random_normal(shape=[input_neurons, nn_architecture[0]]))
offset_1 = tf.Variable(tf.random_normal(shape=[nn_architecture[0]]))
layer_matrix_2 = tf.Variable(tf.random_normal(shape=[nn_architecture[0], nn_architecture[1]]))
offset_2 = tf.Variable(tf.random_normal(shape=[nn_architecture[1]]))
layer_matrix_3 = tf.Variable(tf.random_normal(shape=[nn_architecture[1], output_neurons]))
offset_3 = tf.Variable(tf.random_normal(shape=[output_neurons]))
# Creating hidden layers and output
layer_1 = tf.nn.elu(tf.matmul(input_classical_layer, layer_matrix_1) + offset_1)
layer_2 = tf.nn.elu(tf.matmul(layer_1, layer_matrix_2) + offset_2)
output_layer = tf.nn.elu(tf.matmul(layer_2, layer_matrix_3) + offset_3)
# ===================================================================================
# Defining QNN parameters
# ===================================================================================
# Number of beamsplitters in interferometer
bs_in_interferometer = int(1.0 * mode_number * (mode_number - 1) / 2)
with tf.name_scope('variables'):
bs_variables = tf.Variable(tf.random_normal(shape=[depth, bs_in_interferometer, 2, 2]
, stddev=sdev))
phase_variables = tf.Variable(tf.random_normal(shape=[depth, mode_number, 2], stddev=sdev))
sq_magnitude_variables = tf.Variable(tf.random_normal(shape=[depth, mode_number]
, stddev=sdev_photon))
sq_phase_variables = tf.Variable(tf.random_normal(shape=[depth, mode_number]
, stddev=sdev))
disp_magnitude_variables = tf.Variable(tf.random_normal(shape=[depth, mode_number]
, stddev=sdev_photon))
disp_phase_variables = tf.Variable(tf.random_normal(shape=[depth, mode_number]
, stddev=sdev))
kerr_variables = tf.Variable(tf.random_normal(shape=[depth, mode_number], stddev=sdev_photon))
parameters = [layer_matrix_1, offset_1, layer_matrix_2, offset_2, layer_matrix_3, offset_3, bs_variables,
phase_variables, sq_magnitude_variables, sq_phase_variables, disp_magnitude_variables,
disp_phase_variables, kerr_variables]
# ===================================================================================
# Constructing quantum layers
# ===================================================================================
# Defining input QNN layer, whose parameters are set by the outputs of the classical network
def input_qnn_layer():
with tf.name_scope('inputlayer'):
Sgate(tf.clip_by_value(output_layer[:, 0], -sq_clip, sq_clip), output_layer[:, 1]) | q[0]
Sgate(tf.clip_by_value(output_layer[:, 2], -sq_clip, sq_clip), output_layer[:, 3]) | q[1]
BSgate(output_layer[:, 4], output_layer[:, 5]) | (q[0], q[1])
Rgate(output_layer[:, 6]) | q[0]
Rgate(output_layer[:, 7]) | q[1]
Dgate(tf.clip_by_value(output_layer[:, 8], -disp_clip, disp_clip), output_layer[:, 9]) \
| q[0]
Dgate(tf.clip_by_value(output_layer[:, 10], -disp_clip, disp_clip), output_layer[:, 11]) \
| q[1]
Kgate(tf.clip_by_value(output_layer[:, 12], -kerr_clip, kerr_clip)) | q[0]
Kgate(tf.clip_by_value(output_layer[:, 13], -kerr_clip, kerr_clip)) | q[1]
# Defining standard QNN layers
def qnn_layer(layer_number):
with tf.name_scope('layer_{}'.format(layer_number)):
BSgate(bs_variables[layer_number, 0, 0, 0], bs_variables[layer_number, 0, 0, 1]) \
| (q[0], q[1])
for i in range(mode_number):
Rgate(phase_variables[layer_number, i, 0]) | q[i]
for i in range(mode_number):
Sgate(tf.clip_by_value(sq_magnitude_variables[layer_number, i], -sq_clip, sq_clip),
sq_phase_variables[layer_number, i]) | q[i]
BSgate(bs_variables[layer_number, 0, 1, 0], bs_variables[layer_number, 0, 1, 1]) \
| (q[0], q[1])
for i in range(mode_number):
Rgate(phase_variables[layer_number, i, 1]) | q[i]
for i in range(mode_number):
Dgate(tf.clip_by_value(disp_magnitude_variables[layer_number, i], -disp_clip,
disp_clip), disp_phase_variables[layer_number, i]) | q[i]
for i in range(mode_number):
Kgate(tf.clip_by_value(kerr_variables[layer_number, i], -kerr_clip, kerr_clip)) | q[i]
# ===================================================================================
# Defining QNN
# ===================================================================================
# construct the two-mode Strawberry Fields engine
eng, q = sf.Engine(mode_number)
# construct the circuit
with eng:
input_qnn_layer()
for i in range(depth):
qnn_layer(i)
# run the engine (in batch mode)
state = eng.run("tf", cutoff_dim=cutoff, eval=False, batch_size=batch_size)
# extract the state
ket = state.ket()
@Maria_Schuld Now in the defining QNN part I am getting the error. I have to change the tensorflow version as the placeholder method is not working with current TF version. My strawberryField version is -
Strawberry Fields: a Python library for continuous-variable quantum circuits.
Copyright 2018-2020 Xanadu Quantum Technologies Inc.
Python version: 3.7.10
Platform info: Linux-5.4.109±x86_64-with-Ubuntu-18.04-bionic
Installation path: /usr/local/lib/python3.7/dist-packages/strawberryfields
Strawberry Fields version: 0.18.0
Numpy version: 1.19.5
Scipy version: 1.4.1
SymPy version: 1.7.1
NetworkX version: 2.5.1
The Walrus version: 0.15.1
Blackbird version: 0.3.0
TensorFlow version: 2.4.1