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 shadows.py and repair the typo in gates.py #169

Merged
merged 20 commits into from
Aug 3, 2023

Conversation

PeilinZHENG
Copy link
Contributor

No description provided.

@refraction-ray
Copy link
Contributor

please add type annotation, test files

c_.R(i, theta=theta, alpha=alpha, phi=phi)
return c_.sample(batch=repeat, format="sample_bin")

vpm = backend.vmap(proj_measure, vectorized_argnums=0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jit outside vmap


v = backend.vmap(tensor_prod, vectorized_argnums=0)
vv = backend.vmap(v, vectorized_argnums=0)
return vv(shadow_states)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a final sum lacking?

new_indices = f"{ABC[0:2 * nq:2]}{ABC[1:2 * nq:2]}"

def tensor_prod(dms):
return backend.reshape(backend.einsum(f'{",".join(old_indices)}->{new_indices}', *dms, optimize=True),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure whether every backend has optimizer=True argument, to be checked

expc, ent, lss_states, sdw_state = csjit(c.state(), pauli_strings, status, sub, x, y, z)
expc = np.median(expc)

assert np.abs(expc - exact) < 0.1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add entropy assert

def classical_shadow(psi, pauli_strings, status, sub, x, y, z):
snapshots = shadow_snapshots(psi, pauli_strings, status)
lss_states = local_snapshot_states(snapshots, pauli_strings)
sdw_state = global_shadow_state(lss_states, sub=sub)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could also add a test to assert close between global state from local states and snapshots directly

sdw_state = global_shadow_state(lss_states)

R = np.array(sdw_state - bell_state)
assert np.sqrt(np.trace(R.conj().T @ R)) < 0.1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

np.testing.assert_allclose could directly assert the closeness of two matrix elementwisely

@codecov
Copy link

codecov bot commented Aug 3, 2023

Codecov Report

Merging #169 (fbe5a9e) into master (e05b75c) will decrease coverage by 0.06%.
The diff coverage is 71.09%.

@@            Coverage Diff             @@
##           master     #169      +/-   ##
==========================================
- Coverage   75.04%   74.99%   -0.06%     
==========================================
  Files          66       67       +1     
  Lines       10167    10340     +173     
==========================================
+ Hits         7630     7754     +124     
- Misses       2537     2586      +49     
Files Changed Coverage Δ
tensorcircuit/gates.py 96.23% <ø> (ø)
tensorcircuit/shadows.py 70.93% <70.93%> (ø)
tensorcircuit/quantum.py 78.38% <100.00%> (+0.17%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

lss_states = shadow_snapshots(c.state(), pauli_strings, status)
sdw_state = global_shadow_state(lss_states)

np.allclose(sdw_state, bell_state, atol=0.01)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

np.tesing.assert_allclose()

Copy link
Contributor

@refraction-ray refraction-ray left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks for the contribution. TC will now have jittable and vmappable implementation for classical shadows

@refraction-ray refraction-ray merged commit fbe5a9e into tencent-quantum-lab:master Aug 3, 2023
2 of 4 checks passed
@refraction-ray
Copy link
Contributor

@all-contributors please add @PeilinZHENG for code, tests, docs

@allcontributors
Copy link
Contributor

@refraction-ray

I've put up a pull request to add @PeilinZHENG! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants