def test_inner(self, backend): psi = peps.computational_zeros(2, 3, backend=backend) psi.apply_circuit([ Gate('H', [], [0]), Gate('CX', [], [0, 3]), Gate('H', [], [3]), ]) phi = peps.computational_zeros(2, 3, backend=backend) self.assertTrue(np.isclose(psi.inner(phi), 0.5))
def test_inner_approx(self, backend): psi = peps.computational_zeros(2, 3, backend=backend) psi.apply_circuit([ Gate('H', [], [0]), Gate('CX', [], [0,3]), Gate('H', [], [3]), ], update_option=peps.DirectUpdate(ImplicitRandomizedSVD(rank=2))) phi = peps.computational_zeros(2, 3, backend=backend) contract_option = peps.BMPS(ReducedSVD(rank=2)) self.assertTrue(backend.isclose(psi.inner(phi, contract_option), 0.5))
def test_amplitude_nonlocal(self, backend): update_options = [ None, peps.DirectUpdate(ImplicitRandomizedSVD(rank=2)), peps.QRUpdate(rank=2), peps.LocalGramQRUpdate(rank=2), peps.LocalGramQRSVDUpdate(rank=2), ] for option in update_options: with self.subTest(update_option=option): qstate = peps.computational_zeros(2, 3, backend=backend) qstate.apply_circuit([ Gate('X', [], [0]), Gate('H', [], [1]), Gate('CX', [], [0, 5]), Gate('CX', [], [1, 3]), Gate('S', [], [1]), ], update_option=option) self.assertTrue( np.isclose(qstate.amplitude([1, 0, 0, 0, 0, 1]), 1 / np.sqrt(2))) self.assertTrue( np.isclose(qstate.amplitude([1, 1, 0, 1, 0, 1]), 1j / np.sqrt(2)))
def test_amplitude(self, backend): qstate = peps.computational_zeros(2, 3, backend=backend) qstate.apply_circuit([ Gate('X', [], [0]), Gate('H', [], [1]), Gate('CX', [], [0,3]), Gate('CX', [], [1,4]), Gate('S', [], [1]), ]) self.assertTrue(backend.isclose(qstate.amplitude([1,0,0,1,0,0]), 1/np.sqrt(2))) self.assertTrue(backend.isclose(qstate.amplitude([1,1,0,1,1,0]), 1j/np.sqrt(2)))
def test_probablity(self, backend): qstate = peps.computational_zeros(2, 3, backend=backend) qstate.apply_circuit([ Gate('X', [], [0]), Gate('H', [], [1]), Gate('CX', [], [0,3]), Gate('CX', [], [1,4]), Gate('S', [], [1]), ]) self.assertTrue(backend.isclose(qstate.probability([1,0,0,1,0,0]), 1/2)) self.assertTrue(backend.isclose(qstate.probability([1,1,0,1,1,0]), 1/2))
def test_amplitude_local_gram_qr_svd_update(self, backend): qstate = peps.computational_zeros(2, 3, backend=backend) qstate.apply_circuit([ Gate('X', [], [0]), Gate('H', [], [1]), Gate('CX', [], [0,3]), Gate('CX', [], [1,4]), Gate('S', [], [1]), ], update_option=peps.LocalGramQRSVDUpdate(rank=2)) contract_option = peps.BMPS(ReducedSVD(rank=2)) self.assertTrue(backend.isclose(qstate.amplitude([1,0,0,1,0,0], contract_option), 1/np.sqrt(2))) self.assertTrue(backend.isclose(qstate.amplitude([1,1,0,1,1,0], contract_option), 1j/np.sqrt(2)))
def test_amplitude_approx(self, backend): qstate = peps.computational_zeros(2, 3, backend=backend) qstate.apply_circuit([ Gate('X', [], [0]), Gate('H', [], [1]), Gate('CX', [], [0,3]), Gate('CX', [], [1,4]), Gate('S', [], [1]), ], update_option=peps.DirectUpdate(ImplicitRandomizedSVD(rank=2))) contract_option = peps.BMPS(ReducedSVD(rank=2)) self.assertTrue(backend.isclose(qstate.amplitude([1,0,0,1,0,0], contract_option), 1/np.sqrt(2))) self.assertTrue(backend.isclose(qstate.amplitude([1,1,0,1,1,0], contract_option), 1j/np.sqrt(2)))
def test_expectation(self, backend): qstate = peps.computational_zeros(2, 3, backend=backend) qstate.apply_circuit([ Gate('X', [], [0]), Gate('CX', [], [0, 3]), Gate('H', [], [2]), ]) observable = 1.5 * Observable.sum([ Observable.Z(0) * 2, Observable.Z(1), Observable.Z(2) * 2, Observable.Z(3), ]) self.assertTrue(np.isclose(qstate.expectation(observable), -3))
def test_norm(self, backend): qstate = peps.computational_zeros(2, 3, backend=backend) qstate.apply_circuit([ Gate('X', [], [0]), Gate('H', [], [1]), Gate('CX', [], [0, 3]), Gate('CX', [], [1, 4]), Gate('S', [], [1]), ]) self.assertTrue(np.isclose(qstate.norm(), 1)) qstate *= 2 self.assertTrue(np.isclose(qstate.norm(), 2)) qstate /= 2j self.assertTrue(np.isclose(qstate.norm(), 1))
def test_expectation_use_cache_approx(self, backend): qstate = peps.computational_zeros(2, 3, backend=backend) qstate.apply_circuit([ Gate('X', [], [0]), Gate('CX', [], [0,3]), Gate('H', [], [2]), ], update_option=peps.DirectUpdate(ImplicitRandomizedSVD(rank=2))) observable = 1.5 * Observable.sum([ Observable.Z(0) * 2, Observable.Z(1), Observable.Z(2) * 2, Observable.Z(3), ]) contract_option = peps.BMPS(ReducedSVD(rank=2)) self.assertTrue(backend.isclose(qstate.expectation(observable, use_cache=True, contract_option=contract_option), -3))
def test_expectation_single_layer(self, backend): qstate = peps.computational_zeros(2, 3, backend=backend) qstate.apply_circuit([ Gate('X', [], [0]), Gate('CX', [], [0,3]), Gate('H', [], [2]), ]) observable = 1.5 * Observable.sum([ Observable.Z(0) * 2, Observable.Z(1), Observable.Z(2) * 2, Observable.Z(3), ]) contract_option = peps.SingleLayer(ImplicitRandomizedSVD(rank=2)) self.assertTrue(backend.isclose(qstate.expectation(observable, contract_option=contract_option), -3))
def test_expectation_use_cache(self, backend): qstate = peps.computational_zeros(2, 3, backend=backend) qstate.apply_circuit([ Gate('X', [], [0]), Gate('CX', [], [0,3]), Gate('H', [], [2]), ]) observable = 1.5 * Observable.sum([ Observable.Z(0) * 2, Observable.Z(1), Observable.Z(2) * 2, Observable.Z(3), ]) self.assertTrue(backend.isclose(qstate.expectation(observable, use_cache=True), -3)) cache = peps.make_expectation_cache(qstate, qstate) self.assertTrue(backend.isclose(qstate.expectation(observable, use_cache=cache), -3)) self.assertTrue(backend.isclose(qstate.norm(cache=cache), 1))
def test_add(self, backend): psi = peps.computational_zeros(2, 3, backend=backend) phi = peps.computational_ones(2, 3, backend=backend) self.assertTrue(np.isclose((psi + phi).norm(), np.sqrt(2)))