-
Notifications
You must be signed in to change notification settings - Fork 31
/
test_observables.py
140 lines (104 loc) · 3.74 KB
/
test_observables.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# Copyright 2019 PIQuIL - All Rights Reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from math import isclose
import pytest
import torch
import qucumber.observables as observables
from qucumber.nn_states import WaveFunctionBase
class MockWaveFunction(WaveFunctionBase):
_rbm_am = None
_device = None
def __init__(self, nqubits, state=None):
self.nqubits = nqubits
self.device = "cpu"
def sample(self, num_samples=1):
dist = torch.distributions.Bernoulli(probs=0.5)
sample_size = torch.Size((num_samples, self.nqubits))
initial_state = dist.sample(sample_size).to(
device=self.device, dtype=torch.double
)
return initial_state
@staticmethod
def autoload(location, gpu=False):
pass
def gradient(self, v):
return 0.0
@property
def rbm_am(self):
return self._rbm_am
@rbm_am.setter
def rbm_am(self, new_val):
self._rbm_am = new_val
@property
def device(self):
return self._device
@device.setter
def device(self, new_val):
self._device = new_val
@property
def networks(self):
return ["rbm_am"]
def phase(self, v):
if v.dim() == 1:
v = v.unsqueeze(0)
unsqueezed = True
else:
unsqueezed = False
phase = torch.zeros(v.shape[0])
if unsqueezed:
return phase.squeeze_(0)
else:
return phase
def amplitude(self, v):
return torch.ones(v.size(0)) / torch.sqrt(torch.tensor(float(self.nqubits)))
def psi(self, v):
# vector/tensor of shape (len(v),)
amplitude = self.amplitude(v)
# complex vector; shape: (2, len(v))
psi = torch.zeros((2,) + amplitude.shape).to(
dtype=torch.double, device=self.device
)
psi[0] = amplitude
# squeeze down to complex scalar if there was only one visible state
return psi.squeeze()
@pytest.fixture(scope="module")
def mock_wavefunction_samples(request):
test_psi = MockWaveFunction(2)
test_sample = test_psi.sample(num_samples=100000)
return test_psi, test_sample
def test_spinflip(mock_wavefunction_samples):
test_psi, test_sample = mock_wavefunction_samples
samples = test_sample.clone()
observables.pauli.flip_spin(1, samples) # flip spin
observables.pauli.flip_spin(1, samples) # flip it back
assert torch.equal(samples, test_sample)
paulis = [
pytest.param((observables.SigmaX, 1.0, 1.0), id="X"),
pytest.param((observables.SigmaY, 0.0, 0.0), id="Y"),
pytest.param((observables.SigmaZ, 0.0, 0.5), id="Z"),
]
@pytest.mark.parametrize("pauli", paulis)
@pytest.mark.parametrize(
"absolute", [pytest.param(True, id="absolute"), pytest.param(False, id="signed")]
)
def test_pauli(mock_wavefunction_samples, pauli, absolute):
test_psi, test_sample = mock_wavefunction_samples
pauli_op, mag, abs_mag = pauli
if absolute:
mag = abs_mag
prefix = "(absolute) "
else:
prefix = ""
obs = pauli_op(absolute=absolute)
measure_O = float(obs.apply(test_psi, test_sample).mean())
assert isclose(
mag, measure_O, abs_tol=1e-2
), "measure {}-magnetization failed".format(prefix + obs.symbol)