-
Notifications
You must be signed in to change notification settings - Fork 158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add MPSStateSpace.Sample and its unit test for small case. #460
Conversation
@@ -586,6 +586,51 @@ TEST(MPSStateSpaceTest, InnerProduct4) { | |||
EXPECT_NEAR(f, 0.5524, 1e-4); | |||
} | |||
|
|||
TEST(MPSStateSpaceTest, SamplingSmall) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we maybe add another test with other states, perhaps a simple GHZ or bit flipped state as well ?
const MPS& state, uint64_t num_samples, unsigned seed) const { | ||
std::vector<uint64_t> bitstrings; | ||
|
||
if (num_samples > 0) { | ||
double norm = 0; | ||
uint64_t size = MinSize(state.num_qubits()) / 2; | ||
|
||
const fp_type* p = state.get(); | ||
|
||
for (uint64_t k = 0; k < size; ++k) { | ||
auto re = p[2 * k]; | ||
auto im = p[2 * k + 1]; | ||
norm += re * re + im * im; | ||
} | ||
|
||
auto rs = GenerateRandomValues<DistrRealType>(num_samples, seed, norm); | ||
|
||
uint64_t m = 0; | ||
double csum = 0; | ||
bitstrings.reserve(num_samples); | ||
|
||
for (uint64_t k = 0; k < size; ++k) { | ||
auto re = p[2 * k]; | ||
auto im = p[2 * k + 1]; | ||
csum += re * re + im * im; | ||
while (rs[m] < csum && m < num_samples) { | ||
bitstrings.emplace_back(k); | ||
++m; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a little worried about this implementation of Sample(). If a user were to make an MPS on 64 qubits or more, I think the uint64_t type might overflow. Also, Is this logic used here correct ? Looking at the implementation done here (https://github.com/quantumlib/Cirq/blob/master/cirq-core/cirq/contrib/quimb/mps_simulator.py#L469) it looks like we should be tracing out qubits as we go and assigning them values for each sample we draw. Are we still doing this here ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes good catch, thank you for the standard process of MPS measurement. I need to add the contraction process (tracing out). Let me work on it more.
Going to close this now since it is done in #490 . |
Resolve #441