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
| # 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
| # 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
| #!/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
| 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
| 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
| 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
| # The backend simulator available to me | |
| backend = qk.IBMQ.get_backend('ibmqx4') |
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
| ''' | |
| `n` is the number of Qubits needed to | |
| generate a a random number between | |
| 0 and 2^n - 1 | |
| ''' | |
| n = 3 | |
| ''' | |
| Creating a Quantum Register with `n` Qubits and | |
| `n` Classical Bits where n=3 |
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
| ''' | |
| Applying a Hadamard Gate on the n Qubits | |
| to get a final bitstring of size n | |
| The bitstring will be converted to a | |
| decimal number (integer) between 0 and 2^3 - 1 (7) | |
| ''' | |
| for i in range(n): | |
| circ.h(q[i]) | |
| circ.measure(q,c) |