This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def rand_int(): | |
| new_job = qk.execute(circ, backend, shots=1) | |
| # The output bitstring consists of 3 collapsed Qubits (bits) | |
| bitstring = new_job.results().get_counts() | |
| bitstring = list(bitstring.keys()[0]) | |
| # Converting binary to Decimal integers | |
| random_integer = int(bitstring, 2) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import qiskit as qk | |
| # Load saved account from memory | |
| qk.IBMQ.load_accounts() | |
| n = 3 | |
| q = qk.QuantumRegister(n) | |
| c = qk.ClassicalRegister(n) | |
| circ = qk.QuantumCircuit(q, c) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| a = rand_int() | |
| print (a) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import os | |
| from flask import Flask, request | |
| from interactive_conditional_samples import interact_model | |
| app = Flask(__name__) | |
| @app.route('/', methods=['GET']) | |
| def hello(): | |
| return "Hello from GPT-2 model server!" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Here, we have added a `raw_text` parameter | |
| # From our Flask API we'll pass in the incoming text into this function | |
| def interact_model(raw_text, model_name='117M', seed=None, nsamples=1, batch_size=1, length=None, temperature=1, top_k=40, models_dir='models/'): | |
| models_dir = os.path.expanduser(os.path.expandvars(models_dir)) | |
| if batch_size is None: | |
| batch_size = 1 | |
| assert nsamples % batch_size == 0 | |
| enc = encoder.get_encoder(model_name, models_dir) | |
| hparams = model.default_hparams() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Detect hardware | |
| try: | |
| tpu = tf.distribute.cluster_resolver.TPUClusterResolver() # TPU detection | |
| except ValueError: # If TPU not found, resolve to GPU instead | |
| tpu = None | |
| gpus = tf.config.experimental.list_logical_devices("GPU") | |
| # Select appropriate distribution strategy | |
| if tpu: | |
| tf.tpu.experimental.initialize_tpu_system(tpu) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| resolver = tf.contrib.cluster_resolver.TPUClusterResolver(tpu='grpc://' + os.environ['COLAB_TPU_ADDR']) | |
| tf.contrib.distribute.initialize_tpu_system(resolver) | |
| strategy = tf.contrib.distribute.TPUStrategy(resolver) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| with strategy.scope(): | |
| """ | |
| This essentailly takes our model and makes it | |
| compatible to train on a TPU. | |
| """ | |
| model = tf.keras.models.Sequential() | |
| model.add(tf.keras.layers.Dense(512, input_size=[784,], activation='relu')) | |
| model.add(tf.keras.layers.Dense(256, activation='relu')) | |
| model.add(tf.keras.layers.Dense(128, activation='relu')) | |
| model.add(tf.keras.layers.Dense(64, activation='relu')) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| history = model.fit(x_train, | |
| y_train, | |
| epochs=20, | |
| steps_per_epoch=50 | |
| ) | |
| model.save_weights('./mnist_model.h5', overwrite=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Detect hardware | |
| try: | |
| tpu = tf.distribute.cluster_resolver.TPUClusterResolver() # TPU detection | |
| except ValueError: # If TPU not found | |
| tpu = None |