-
Notifications
You must be signed in to change notification settings - Fork 79
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
Add shadows.py and repair the typo in gates.py #169
Conversation
local synchronization
local synchronization
please add type annotation, test files |
tensorcircuit/shadows.py
Outdated
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) |
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.
jit outside vmap
tensorcircuit/shadows.py
Outdated
|
||
v = backend.vmap(tensor_prod, vectorized_argnums=0) | ||
vv = backend.vmap(v, vectorized_argnums=0) | ||
return vv(shadow_states) |
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.
is there a final sum lacking?
tensorcircuit/shadows.py
Outdated
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), |
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.
not sure whether every backend has optimizer=True argument, to be checked
tests/test_shadows.py
Outdated
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 |
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.
add entropy assert
tests/test_shadows.py
Outdated
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) |
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 also add a test to assert close between global state from local states and snapshots directly
tests/test_shadows.py
Outdated
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 |
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.
np.testing.assert_allclose could directly assert the closeness of two matrix elementwisely
add gitattr to auto lr
Codecov Report
@@ 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
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
tests/test_shadows.py
Outdated
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) |
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.
np.tesing.assert_allclose()
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.
LGTM. Thanks for the contribution. TC will now have jittable and vmappable implementation for classical shadows
fbe5a9e
into
tencent-quantum-lab:master
@all-contributors please add @PeilinZHENG for code, tests, docs |
I've put up a pull request to add @PeilinZHENG! 🎉 |
No description provided.