Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Tensorflow Backend Questions #193

Answered by refraction-ray
wcqc asked this question in Q&A
Discussion options

Hi,

Two questions about using TC with the TensorFlow backend:

  1. When using the Tensorflow backend and keras.Sequential, does TensorCircuit support putting additional Keras layers before the qml_layer, such as a dense layer to transform the input before being fed to a qml_layer defined in TC? If yes, would it be possible to let TC fine-tune the input embeddings? Something like this:
model = tf.keras.Sequential()
model.add(tf.keras.layers.Embedding(100, 5))
model.add(tf.keras.layers.Dense(num_params))
model.add(qml_layer)
model.add(tf.keras.layers.Dense(1, activation="sigmoid"))
  1. What's the best way to debug and print the variables in the debugger when using the TF backend? tf.print() doesn't seem to work?

Thanks!

You must be logged in to vote
K = tc.set_backend("tensorflow")
def f(param, weights):
 c = tc.Circuit(2)
 print(param)
 return 1.0
qmllayer = tc.keras.HardwareLayer(f, [2])
qmllayer(K.ones([2]))

what about this, HardwareLayer is a non jit version wrapper for keras layer, after you debug, you can switch back to KerasLayer

Replies: 1 comment 4 replies

Comment options

  1. of course, qmllayer is just a keras layer, you can compose any computational graph with all kinds of classical and quantum layers in one model
  2. you can always directly print(model.variables) outside a jitted function?
You must be logged in to vote
4 replies
Comment options

Thanks! Is there a good recommended way to debug and print within a quantum layer such as (when used as a Keras layer -- set run_eagerly=True doesn't seem to help):

def qlayer(x):
 c = tc.Circuit(n, inputs=x)
### print here???
 for j in range(max_sent_len):
 for i in range(n):
 c.rx(i, theta=x[i])
Comment options

K = tc.set_backend("tensorflow")
def f(param, weights):
 c = tc.Circuit(2)
 print(param)
 return 1.0
qmllayer = tc.keras.HardwareLayer(f, [2])
qmllayer(K.ones([2]))

what about this, HardwareLayer is a non jit version wrapper for keras layer, after you debug, you can switch back to KerasLayer

Answer selected by wcqc
Comment options

One quick additional question:

If the JAX backend is used, would it be possible to use jax.lax.scan or jax.lax.fori_loop in place of standard for loops to speed up circuit compile especially for deep circuits?

Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /