예제 #1
0
    def gen(self, ham, itr=1000, nbasis=1000, burn=500, thin=1):
        _, state = ground(ham)
        n = ham.lattice.numel()

        try:
            # os.makedirs(os.path.join(self.root, self.raw_folder))
            os.makedirs(os.path.join(self.root, self.processed_folder))
        except OSError as e:
            if e.errno == errno.EEXIST:
                pass
            else:
                raise

        data = []
        basis = []
        for _ in range(nbasis):
            ops = MBOp(choice([sigmax, sigmay, sigmaz]) for i in range(n))
            _state = ops.invtrans(state)
            sampler = STMetropolis(proposal=lambda x: abs(_state[bin(x)])**2,
                                   size=ham.size)
            data.extend(sampler.sample(itr=itr, burn=burn, thin=thin))
            basis.extend(
                torch.FloatTensor(ops.params()).resize_(2 * n)
                for i in range(len(sampler.collector)))
        return data, basis
예제 #2
0
def gen_tomo_data(n, name):
    h = Ham(name, length=n, J=(1.0, 1.0), pbc=True)
    _, state = ground(h)
    ops = MBOp(choice([sigmax, sigmay, sigmaz]) for i in range(n))
    _state = ops.invtrans(state)
    sampler = STMetropolis(proposal=lambda x: _state[bin(x)], size=h.size)
    data = sampler.sample(itr=1000, burn=500, thin=1)
    return [ops.params(), list(data)]
예제 #3
0
 def generate(self):
     data = []
     basis = []
     for _ in range(self.nbasis):
         ops = MBOp(choice([sigmax, sigmay, sigmaz]) for i in range(self.n))
         _state = ops.invtrans(self.state)
         sampler = STMetropolis(proposal=lambda x: abs(_state[bin(x)])**2,
                                size=self.size)
         data.extend(
             sampler.sample(itr=self.itr, burn=self.burn, thin=self.thin))
         basis.extend(
             torch.FloatTensor(ops.params()).resize_(2 * self.n)
             for i in range(len(sampler.collector)))
     return data, basis
예제 #4
0
    def _test_energy(self, h):
        energy, state = utils.ed(h.mat())
        exact = energy[0]
        state = state[:, 0]

        def _fake_proposal(x):
            return abs(state[utils.bin(x)])**2

        def _fake_ansatz(x):
            return state[utils.bin(x)]

        def _local_energy(x):
            ret = sum(val * _fake_ansatz(config) for config, val in h.nnz(x))
            return ret / _fake_ansatz(x)

        sampler = STMetropolis(_fake_proposal, h.size)
        collector = sampler.sample(itr=10000, burn=500, thin=1)
        energy = sum(_local_energy(config) for config in collector)
        energy /= len(collector)
        self.assertLess(abs(energy - exact), 0.05)
예제 #5
0
print(eigs[0])


def eloc(x):
    ret = sum(val * model(Variable(config))
              for config, val in hamiltonian.nnz(x))
    return ret / model(Variable(x))


def proposal(x):
    amp = model(Variable(x))
    return abs(amp.data[0])**2


collector = STCollector(merge=True, params=model.parameters())
sampler = STMetropolis(proposal=proposal, size=size, collector=collector)
optimizer = optim.Adam(model.parameters())

energy_cache = []

for epoch in range(1000):
    print('epoch %s' % epoch)
    collector.clear()
    sampler.sample(**train_sampler)
    energy = 0.0
    for config in collector:
        loss = -eloc(config)
        energy += -loss
        loss.backward()
        optimizer.step()
예제 #6
0
    ret = sum(val * model(Variable(config))
              for config, val in hamiltonian.nnz(x))
    return ret / model(Variable(x))


def exact_energy():
    return sum(torch.abs(model(Variable(config)))**2 * eloc(config))


def proposal(x):
    amp = model(Variable(x))
    return abs(amp.data[0])**2


collector = STCollector(merge=True, params=model.parameters())
sampler = STMetropolis(proposal=proposal, size=3, collector=collector)
optimizer = optim.Adam(model.parameters())
energy_cache = []

for epoch in range(1000):
    collector.clear()
    configs = sampler.sample(itr=1000, burn=500, thin=1)
    energy = 0.0
    for config in configs:
        loc = eloc(config)
        energy += loc
        collector.zero_grad()
        psi = model(Variable(config))
        psi.backward()
        collector.collect(gradient, eloc=loc)
        # or