Пример #1
0
 def test_contract_scalar(self, backend):
     qstate = peps.random(3, 4, 2, backend=backend)
     norm = qstate.norm(contract_option=Snake())
     for contract_option in contract_options:
         if contract_option is not Snake:
             for svd_option in (None, ReducedSVD(16), RandomizedSVD(16),
                                ImplicitRandomizedSVD(16),
                                ImplicitRandomizedSVD(
                                    16, orth_method='local_gram')):
                 with self.subTest(contract_option=contract_option.__name__,
                                   svd_option=svd_option):
                     self.assertTrue(
                         np.isclose(
                             norm,
                             qstate.norm(contract_option=contract_option(
                                 svd_option))))
 def test_einsumsvd_options(self, tb):
     from tensorbackends.interface import ReducedSVD, RandomizedSVD, ImplicitRandomizedSVD
     a = tb.astensor(
         [[0, 2e-3j, 0, 0], [1e-3, 0, 0, 0], [0, 0, 3, 0], [0, 0, 0, 4j]],
         dtype=complex)
     p = tb.astensor(
         [[0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]],
         dtype=complex)
     s_true = tb.astensor([4, 3])
     low_rank = tb.astensor(
         [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 3, 0], [0, 0, 0, 4j]],
         dtype=complex)
     options = [
         ReducedSVD(rank=2),
         RandomizedSVD(rank=2, niter=2, oversamp=1),
         ImplicitRandomizedSVD(rank=2, niter=2)
     ]
     for option in options:
         with self.subTest(option=option):
             u, s, v = tb.einsumsvd('ij,jk->is,sk', p, a, option=option)
             usv = tb.einsum('is,s,sk->ik', u, s, v)
             self.assertEqual(u.shape, (4, 2))
             self.assertEqual(s.shape, (2, ))
             self.assertEqual(v.shape, (2, 4))
             self.assertTrue(tb.allclose(s, s_true))
             self.assertTrue(tb.allclose(usv, low_rank, atol=1e-9))
Пример #3
0
 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)))
Пример #4
0
 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_einsumsvd_absorb_s(self, tb):
     from tensorbackends.interface import ReducedSVD, RandomizedSVD, ImplicitRandomizedSVD
     a = tb.astensor([[0,2e-3j,0,0],[1e-3,0,0,0],[0,0,3,0],[0,0,0,4j]], dtype=complex)
     p = tb.astensor([[0,1,0,0],[1,0,0,0],[0,0,1,0],[0,0,0,1]], dtype=complex)
     s_true = tb.astensor([4,3])
     low_rank = tb.astensor([[0,0,0,0],[0,0,0,0],[0,0,3,0],[0,0,0,4j]], dtype=complex)
     options = [
         ReducedSVD(rank=2),
         RandomizedSVD(rank=2, niter=2, oversamp=1),
         ImplicitRandomizedSVD(rank=2, niter=2, orth_method='qr'),
         ImplicitRandomizedSVD(rank=2, niter=2, orth_method='local_gram'),
     ]
     for option in options:
         for absorb_s in ['even', 'u', 'v']:
             with self.subTest(option=option):
                 u, _, v = tb.einsumsvd('ij,jk->is,sk', p, a, option=option, absorb_s=absorb_s)
                 usv = tb.einsum('is,sk->ik', u, v)
                 self.assertEqual(u.shape, (4,2))
                 self.assertEqual(v.shape, (2,4))
                 self.assertTrue(tb.allclose(usv, low_rank, atol=1e-9))
Пример #6
0
 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)))
Пример #7
0
 def test_contract_vector(self, backend):
     qstate = peps.random(3, 3, 2, backend=backend)
     statevector = qstate.statevector(contract_option=Snake())
     for contract_option in [
             BMPS(None),
             BMPS(ReducedSVD(16)),
             BMPS(RandomizedSVD(16)),
             BMPS(ImplicitRandomizedSVD(16))
     ]:
         with self.subTest(contract_option=contract_option):
             contract_result = qstate.statevector(
                 contract_option=contract_option)
             self.assertTrue(
                 backend.allclose(statevector.tensor,
                                  contract_result.tensor))
Пример #8
0
 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))
Пример #9
0
 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))