Skip to content

Commit d28e68b

Browse files
trbromleyco9olguy
andauthored
Clarify requirements for running scripts (#10)
* Add requirements * Add hard version checks to script * Add version check and change engine * Add version check script * Add version check * Add version check; * Make requirements in README more central * Add more details to README * Apply suggestions from code review Co-authored-by: Nathan Killoran <co9olguy@users.noreply.github.com> * Simplify logic Co-authored-by: Nathan Killoran <co9olguy@users.noreply.github.com>
1 parent 30e68c7 commit d28e68b

File tree

7 files changed

+65
-23
lines changed

7 files changed

+65
-23
lines changed

README.md

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@ This repository contains the source code used to produce the results presented i
66

77
<br/>
88

9+
## Requirements
10+
11+
To construct and optimize the variational quantum circuits, these scripts and notebooks use the TensorFlow backend of [Strawberry Fields](https://github.com/XanaduAI/strawberryfields). In addition, matplotlib is required for generating output plots.
12+
13+
**Due to subsequent interface upgrades, these scripts will work only with the following
14+
configuration**
15+
16+
- Strawberry Fields version 0.10.0
17+
- TensorFlow version 1.3
18+
- Python version 3.5 or 3.6
19+
20+
Your version of Python can be checked by running `python --version`. The correct versions of
21+
StrawberryFields and TensorFlow can be installed by running `pip install -r requirements.txt`
22+
from the main directory of this repository.
23+
924
## Contents
1025

1126
<!-- <p align="center">
@@ -22,14 +37,6 @@ This repository contains the source code used to produce the results presented i
2237

2338
<img align='right' src="https://github.com/XanaduAI/quantum-neural-networks/blob/master/static/tetronimo_gif.gif">
2439

25-
26-
## Requirements
27-
28-
To construct and optimize the variational quantum circuits, these scripts and notebooks use the TensorFlow backend of [Strawberry Fields](https://github.com/XanaduAI/strawberryfields). In addition, matplotlib is required for generating output plots.
29-
30-
**Due to subsequent interface upgrades, these scripts will work only with Strawberry Fields version <= 0.10.0.**
31-
32-
3340
## Using the scripts
3441

3542
To use the scripts, simply set the input data, output data, and hyperparametersby modifying the scripts directly - and then enter the subdirectory and run the script using Python 3:

fraud_detection/fraud_detection.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
import strawberryfields as sf
2121
from strawberryfields.ops import Dgate, BSgate, Kgate, Sgate, Rgate
2222

23+
import sys
24+
sys.path.append("..")
25+
import version_check
26+
2327
# ===================================================================================
2428
# Hyperparameters
2529
# ===================================================================================
@@ -190,21 +194,18 @@ def qnn_layer(layer_number):
190194
# Defining QNN
191195
# ===================================================================================
192196

193-
# construct the two-mode Strawberry Fields program
194-
prog = sf.Program(mode_number)
197+
# construct the two-mode Strawberry Fields engine
198+
eng, q = sf.Engine(mode_number)
195199

196200
# construct the circuit
197-
with prog.context as q:
201+
with eng:
198202
input_qnn_layer()
199203

200204
for i in range(depth):
201205
qnn_layer(i)
202206

203-
# create an engine
204-
eng = sf.Engine('tf', backend_options={"cutoff_dim": cutoff, "batch_size": batch_size})
205-
206207
# run the engine (in batch mode)
207-
state = eng.run(prog, run_options={"eval": False}).state
208+
state = eng.run("tf", cutoff_dim=cutoff, eval=False, batch_size=batch_size)
208209
# extract the state
209210
ket = state.ket()
210211

fraud_detection/testing.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
import strawberryfields as sf
2121
from strawberryfields.ops import Dgate, BSgate, Kgate, Sgate, Rgate
2222

23+
import sys
24+
sys.path.append("..")
25+
import version_check
26+
2327
# ===================================================================================
2428
# Hyperparameters
2529
# ===================================================================================
@@ -180,21 +184,18 @@ def qnn_layer(layer_number):
180184
# Defining QNN
181185
# ===================================================================================
182186

183-
# construct the two-mode Strawberry Fields program
184-
prog = sf.Program(mode_number)
187+
# construct the two-mode Strawberry Fields engine
188+
eng, q = sf.Engine(mode_number)
185189

186190
# construct the circuit
187-
with prog.context as q:
191+
with eng:
188192
input_qnn_layer()
189193

190194
for i in range(depth):
191195
qnn_layer(i)
192196

193-
# create an engine
194-
eng = sf.Engine('tf', backend_options={"cutoff_dim": cutoff, "batch_size": batch_size})
195-
196197
# run the engine (in batch mode)
197-
state = eng.run(prog, run_options={"eval": False}).state
198+
state = eng.run("tf", cutoff_dim=cutoff, eval=False, batch_size=batch_size)
198199
# extract the state
199200
ket = state.ket()
200201

function_fitting/function_fitting.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
import strawberryfields as sf
2626
from strawberryfields.ops import *
2727

28+
import sys
29+
sys.path.append("..")
30+
import version_check
2831

2932
# ===================================================================================
3033
# Hyperparameters

requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
strawberryfields==0.10
2+
tensorflow==1.3
3+
matplotlib

tetrominos_learning/tetrominos_learning.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@
1717
from strawberryfields.ops import Dgate, BSgate, Kgate, Sgate, Rgate
1818
import tensorflow as tf
1919
import numpy as np
20-
import matplotlib.pyplot as plt
2120
import time
2221
import os
2322

23+
import sys
24+
sys.path.append("..")
25+
import version_check
26+
2427
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
2528
os.environ['OMP_NUM_THREADS'] = '1'
2629
os.environ['CUDA_VISIBLE_DEVICES'] = '1'

version_check.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""Script for checking the correct versions of Python, StrawberryFields and TensorFlow are being
2+
used."""
3+
import sys
4+
5+
import strawberryfields as sf
6+
import tensorflow as tf
7+
8+
python_version = sys.version_info
9+
sf_version = sf.__version__
10+
tf_version = tf.__version__.split(".")
11+
12+
if python_version < (3, 5) or python_version > (3, 6):
13+
raise SystemError("Your version of python is {}.{}. You must have Python 3.5 or 3.6 installed "
14+
"to run this script.".format(python_version.major, python_version.minor))
15+
16+
if sf_version != "0.10.0":
17+
raise ImportError("An incompatible version of StrawberryFields is installed. You must have "
18+
"StrawberryFields version 0.10 to run this script. To install the correct "
19+
"version, run:\n >>> pip install strawberryfields==0.10")
20+
21+
if not(tf_version[0] == "1" and tf_version[1] == "3"):
22+
raise ImportError("An incompatible version of TensorFlow is installed. You must have "
23+
"TensorFlow version 1.3 to run this script. To install the correct "
24+
"version, run:\n >>> pip install tensorflow==1.3")

0 commit comments

Comments
 (0)