def test_basic_fidelity(self): states1 = random_pure_states((5, 2 ** 4, 1)) states2 = random_pure_states((5, 2 ** 4, 1)) fid1 = TestFidelity.naive_alg(states1, states2) fid2 = fidelity(states1, states2) # This is not precisely the same bc. of the numeric alg of the naive alg. self.assertTrue(almost_equal(fid1, fid2, 1e-3))
def test_append_zeros(self): def naive_impl(states, n): tensor([states] + [s0] * n) states = random_pure_states((10, 2 ** 12, 1)) states = append_zeros(states, 2) print(density_matrix(states))
def test_N5_0_3(self): N = 5 targets = [0, 3] data = random_pure_states((2, 2**N, 1)) l = SWAPLayer(targets) l(data) m1 = l.matrix() m2 = qt_swap(N, targets).full() self.assertTrue(almost_equal(m1, m2))
def test_equivalence_with_manual_dm_then_trace(self): with tf.device('cpu'): states = random_pure_states((1000, 2**8, 1)) t_start1 = time.time() dms1 = trace(density_matrix(states), [4, 5, 6, 7]) # Trace 2 and 3 t_start2 = time.time() dms2 = density_matrix(states, [0, 1, 2, 3]) # Get the dm of 0 and 1 t_end = time.time() self.assertTrue(almost_equal(dms1, dms2)) print('Method 1:', t_end - t_start1) print('Method 2:', t_end - t_start2) print('Abount 10x faster')
def test_swap(self): N = 10 for i in range(5): for j in range(5): if i == j: continue targets = [i, j] data = random_pure_states((2, 2**N, 1)) l = SWAPLayer(targets) l(data) self.assertTrue( almost_equal(l.matrix(), qt_swap(N, targets).full()), f'N = {N}, [{i},{j}]')
from tf_qc.losses import Mean1mFidelity, Mean1mUhlmannFidelity, MeanTraceDistance from tf_qc.models import TwoMemoryDiamondQFT from tf_qc.utils import random_pure_states from tf_qc import complex_type device = 'gpu' # Random normalized vectors n_datapoints = 10000 print('Using:', device) N = 12 # Init. states that are random in the input but |0000> on the 4 ancilla qubits with tf.device(device): vectors = random_pure_states((n_datapoints, 2**N, 1), post_zeros=4) input_states = tf.cast(vectors, complex_type) output_states = input_states # Optimizer and loss lr = 0.05 print('Learning rate:', lr) optimizer = tf.optimizers.Adam(lr) # loss = Mean1mUhlmannFidelity([0, 1, 2, 3], N) subsystem = [0, 1, 2, 3, 4, 5, 6, 7] # loss = Mean1mFidelity(subsystem, true_is_pure_on_sub=True) loss = Mean1mFidelity(subsystem, true_is_pure_on_sub=True) with tf.device(device): for _ in range(100):
def build(self, input_shape): super().build(input_shape) self._matrix = qc.make_gate(self.n_qubits, 'fredkin', self.targets, self.control) def call(self, inputs, **kwargs): return self._matrix @ inputs def matrix(self, **kwargs) -> tf.Tensor: return self._matrix if __name__ == '__main__': from tf_qc.utils import random_pure_states from txtutils import ndtotext_print data = random_pure_states((2, 2**2, 1)) l_u3 = U3Layer() l_u3(data) l_u3.variables[0].assign([0]) l_u3.variables[1].assign([0]) l_u3.variables[2].assign([0]) l_u3.variables[3].assign([0]) l_u3.variables[4].assign([π / 2]) l_u3.variables[5].assign([π]) print(*l_u3.variables, sep='\n') ndtotext_print(l_u3.matrix) data_data = lambda N: tf.keras.layers.Input( (2**N, 1), batch_size=2, dtype=complex_type) data = lambda N: random_pure_states((2, 2**N, 1)) l1 = ULayer()
π = math.pi s000 = tensor([s0, s0, s0]) s001 = tensor([s0, s0, s1]) s010 = tensor([s0, s1, s0]) s011 = tensor([s0, s1, s1]) s100 = tensor([s1, s0, s0]) s101 = tensor([s1, s0, s1]) s110 = tensor([s1, s1, s0]) s111 = tensor([s1, s1, s1]) targets = [0, 1, 2, 3] u_layer = ULayer(targets) data = random_pure_states((10, 2**4, 1), post_zeros=0) data_shape = data.shape u_layer.build(data_shape) def tree_qubit_state(amps): amps = tf.reshape(amps, (-1, 2**3, 1)) return tf.cast(normalize_state_vectors(amps), complex_type) with tf.device('cpu'): lin_size = 9 space = tf.linspace(0., 1., lin_size) space = tf.cast(space, complex_type) input_states = \
def test_random_pure_states(self): states = random_pure_states((10, 2**12, 1), post_zeros=2) print(states)
class OneDiamondQFT_no_inverse(OneDiamondQFT): def __init__(self, model_name): super().__init__(model_name) print(self.layers) self.layers.pop(-1) self.target_inv = QCModel([ILayer()]) self.add(self.target_inv) N = 4 # Random normalized vectors n_datapoints = 1000 # vectors = random_state_vectors(n_datapoints, N, 0) vectors = random_pure_states((n_datapoints, 2**N, 1)) input = tf.cast(vectors, complex_type) # Convert teh output so that is is teh true QFT'ed input true_QFT_gate = QFTLayer() output = true_QFT_gate(input) # Optimizer and loss lr = .1 # beta1 = 0.999 print('Learning rate:', lr) # print('First momentum decay rate:', beta1) # optimizer = RAdamOptimizer(lr, beta1) optimizer = tf.optimizers.Adam(lr) # loss = lambda y_true, y_pred: Mean1mFidelity()(y_true, y_pred) + MeanTraceDistance()(y_true, y_pred)
def setUp(self) -> None: with tf.device('cpu'): self.states1 = random_pure_states((10, 2 ** 4, 1))
def setUp(self) -> None: self.states1 = random_pure_states((10, 2 ** 4, 1), post_zeros=2) self.states2 = random_pure_states((10, 2 ** 4, 1), post_zeros=2) self.states3 = random_pure_states((10, 2 ** 4, 1)) self.targets = list(range(2))
return res, f'{round(time.time() - t1, 10)}s' N = 12 targets = [0, 1, 2, 3, 4, 5, 6, 7] class Model(ApproxUsingInverse): def __init__(self): model = QCModel(layers=[ tf.keras.Input( (2**N, 1), dtype=complex_type, name='input_state'), U3Layer(targets) ]) target = QCModel(layers=[ILayer()]) super(Model, self).__init__(model, target, 'test') data = random_pure_states((16, 2**N, 1), post_zeros=2, seed=0) m = Model() m(data) loss1 = Mean1mFidelity() loss2 = Mean1mUhlmannFidelity(targets, N, optimized=True) loss3 = Mean1mUhlmannFidelity(targets, N, optimized=False) output = m.matrix() @ data l1, t1 = time_oper(lambda: loss1(data, output)) l2, t2 = time_oper(lambda: loss2(data, output)) l3, t3 = time_oper(lambda: loss3(data, output)) print('fidelity', t1, l1) print('Uhlmann0', t2, l2) print('Uhlmann1', t3, l3) assert l1 - l2 < 1e-6 m2 = OneMemoryDiamondQFT()