Skip to content
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

Closed
wants to merge 2 commits into from

Conversation

jaeyoo
Copy link
Contributor

@jaeyoo jaeyoo commented Oct 21, 2021

Resolve #441

@google-cla google-cla bot added the cla: yes Override CLA status to unblock PR. label Oct 21, 2021
@MichaelBroughton MichaelBroughton self-requested a review October 21, 2021 23:08
@@ -586,6 +586,51 @@ TEST(MPSStateSpaceTest, InnerProduct4) {
EXPECT_NEAR(f, 0.5524, 1e-4);
}

TEST(MPSStateSpaceTest, SamplingSmall) {
Copy link
Collaborator

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 ?

Comment on lines +202 to +232
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;
}
}
}
Copy link
Collaborator

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 ?

Copy link
Contributor Author

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.

@MichaelBroughton
Copy link
Collaborator

Going to close this now since it is done in #490 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes Override CLA status to unblock PR.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support MPSStateSpace.Sample() for seamless interface
2 participants