|
16 | 16 | import os
|
17 | 17 | import tensorflow as tf
|
18 | 18 | from tensorflow_quantum.core.ops.load_module import load_module
|
| 19 | +from tensorflow_quantum.core.ops import tfq_utility_ops |
19 | 20 |
|
20 | 21 | MATH_OP_MODULE = load_module(os.path.join("math_ops", "_tfq_math_ops.so"))
|
21 | 22 |
|
@@ -55,3 +56,90 @@ def mps_1d_expectation(programs,
|
55 | 56 | tf.float32),
|
56 | 57 | pauli_sums,
|
57 | 58 | bond_dim=bond_dim)
|
| 59 | + |
| 60 | + |
| 61 | +def mps_1d_sample(programs, |
| 62 | + symbol_names, |
| 63 | + symbol_values, |
| 64 | + num_samples, |
| 65 | + bond_dim=4): |
| 66 | + """Generate samples using the C++ MPS simulator. |
| 67 | +
|
| 68 | + Simulate the final state of `programs` given `symbol_values` are placed |
| 69 | + inside of the symbols with the name in `symbol_names` in each circuit. |
| 70 | + From there we will then sample from the final state. |
| 71 | +
|
| 72 | + Args: |
| 73 | + programs: `tf.Tensor` of strings with shape [batch_size] containing |
| 74 | + the string representations of the circuits to be executed. |
| 75 | + symbol_names: `tf.Tensor` of strings with shape [n_params], which |
| 76 | + is used to specify the order in which the values in |
| 77 | + `symbol_values` should be placed inside of the circuits in |
| 78 | + `programs`. |
| 79 | + symbol_values: `tf.Tensor` of real numbers with shape |
| 80 | + [batch_size, n_params] specifying parameter values to resolve |
| 81 | + into the circuits specified by programs, following the ordering |
| 82 | + dictated by `symbol_names`. |
| 83 | + num_samples: `tf.Tensor` with one element indicating the number of |
| 84 | + samples to draw. |
| 85 | + bond_dim: Integer value used for the bond dimension during simulation. |
| 86 | +
|
| 87 | + Returns: |
| 88 | + A `tf.RaggedTensor` containing the samples taken from each circuit in |
| 89 | + `programs`. |
| 90 | + """ |
| 91 | + padded_samples = MATH_OP_MODULE.tfq_simulate_mps1d_samples( |
| 92 | + programs, |
| 93 | + symbol_names, |
| 94 | + tf.cast(symbol_values, tf.float32), |
| 95 | + num_samples, |
| 96 | + bond_dim=bond_dim) |
| 97 | + |
| 98 | + return tfq_utility_ops.padded_to_ragged(padded_samples) |
| 99 | + |
| 100 | + |
| 101 | +def mps_1d_sampled_expectation(programs, |
| 102 | + symbol_names, |
| 103 | + symbol_values, |
| 104 | + pauli_sums, |
| 105 | + num_samples, |
| 106 | + bond_dim=4): |
| 107 | + """Calculate the expectation value of circuits using samples. |
| 108 | +
|
| 109 | + Simulate the final state of `programs` given `symbol_values` are placed |
| 110 | + inside of the symbols with the name in `symbol_names` in each circuit. |
| 111 | + Them, sample the resulting state `num_samples` times and use these samples |
| 112 | + to compute expectation values of the given `pauli_sums`. |
| 113 | +
|
| 114 | + Args: |
| 115 | + programs: `tf.Tensor` of strings with shape [batch_size] containing |
| 116 | + the string representations of the circuits to be executed. |
| 117 | + symbol_names: `tf.Tensor` of strings with shape [n_params], which |
| 118 | + is used to specify the order in which the values in |
| 119 | + `symbol_values` should be placed inside of the circuits in |
| 120 | + `programs`. |
| 121 | + symbol_values: `tf.Tensor` of real numbers with shape |
| 122 | + [batch_size, n_params] specifying parameter values to resolve |
| 123 | + into the circuits specificed by programs, following the ordering |
| 124 | + dictated by `symbol_names`. |
| 125 | + pauli_sums: `tf.Tensor` of strings with shape [batch_size, n_ops] |
| 126 | + containing the string representation of the operators that will |
| 127 | + be used on all of the circuits in the expectation calculations. |
| 128 | + num_samples: `tf.Tensor` with `num_samples[i][j]` is equal to the |
| 129 | + number of samples to draw in each term of `pauli_sums[i][j]` |
| 130 | + when estimating the expectation. Therefore, `num_samples` must |
| 131 | + have the same shape as `pauli_sums`. |
| 132 | + bond_dim: Integer value used for the bond dimension during simulation. |
| 133 | +
|
| 134 | + Returns: |
| 135 | + `tf.Tensor` with shape [batch_size, n_ops] that holds the |
| 136 | + expectation value for each circuit with each op applied to it |
| 137 | + (after resolving the corresponding parameters in). |
| 138 | + """ |
| 139 | + return MATH_OP_MODULE.tfq_simulate_mps1d_sampled_expectation( |
| 140 | + programs, |
| 141 | + symbol_names, |
| 142 | + tf.cast(symbol_values, tf.float32), |
| 143 | + pauli_sums, |
| 144 | + tf.cast(num_samples, dtype=tf.int32), |
| 145 | + bond_dim=bond_dim) |
0 commit comments