Skip to content

Commit

Permalink
Update classical shadows (#708)
Browse files Browse the repository at this point in the history
* More

* typo

* Trigger CI

* lightning req no depth

* Trigger CI
  • Loading branch information
rmoyard authored Feb 23, 2023
1 parent ab34e2a commit e8811cd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
26 changes: 16 additions & 10 deletions demonstrations/tutorial_classical_shadows.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ def calculate_classical_shadow(circuit_template, params, shadow_size, num_qubits
# set up a two-qubit device with shots = 1 to ensure that we only get a single measurement
dev = qml.device("default.qubit", wires=num_qubits, shots=1)


# simple circuit to prepare rho
@qml.qnode(dev)
def local_qubit_rotation_circuit(params, **kwargs):
Expand Down Expand Up @@ -352,6 +353,7 @@ def shadow_state_reconstruction(shadow):

dev = qml.device("default.qubit", wires=num_qubits, shots=1)


# circuit to create a Bell state and measure it in
# the bases specified by the 'observable' keyword argument.
@qml.qnode(dev)
Expand Down Expand Up @@ -387,6 +389,7 @@ def bell_state_circuit(params, **kwargs):

bell_state = np.array([[0.5, 0, 0, 0.5], [0, 0, 0, 0], [0, 0, 0, 0], [0.5, 0, 0, 0.5]])


##############################################################################
# To measure the closeness we can use the operator norm.

Expand Down Expand Up @@ -435,6 +438,7 @@ def operator_2_norm(R):
plt.ylabel("Distance")
plt.show()


##############################################################################
# As expected, when the number of snapshots increases, the state reconstruction
# becomes closer to the ideal state.
Expand Down Expand Up @@ -520,8 +524,8 @@ def estimate_shadow_obervable(shadow, observable, k=10):

# assign the splits temporarily
b_lists_k, obs_lists_k = (
b_lists[i : i + shadow_size // k],
obs_lists[i : i + shadow_size // k],
b_lists[i: i + shadow_size // k],
obs_lists[i: i + shadow_size // k],
)

# find the exact matches for the observable of interest at the specified locations
Expand Down Expand Up @@ -581,8 +585,7 @@ def shadow_bound(error, observables, failure_rate=0.01):
dev = qml.device("default.qubit", wires=num_qubits, shots=1)


@qml.qnode(dev)
def circuit(params, **kwargs):
def circuit_base(params, **kwargs):
observables = kwargs.pop("observable")
for w in range(num_qubits):
qml.Hadamard(wires=w)
Expand All @@ -594,6 +597,8 @@ def circuit(params, **kwargs):
return [qml.expval(o) for o in observables]


circuit = qml.QNode(circuit_base, dev)

params = np.random.randn(2 * num_qubits)

##############################################################################
Expand All @@ -604,9 +609,9 @@ def circuit(params, **kwargs):
# O = \sum_{i=0}^{n-1} X_i X_{i+1} + Y_i Y_{i+1} + Z_i Z_{i+1}.

list_of_observables = (
[qml.PauliX(i) @ qml.PauliX(i + 1) for i in range(num_qubits - 1)]
+ [qml.PauliY(i) @ qml.PauliY(i + 1) for i in range(num_qubits - 1)]
+ [qml.PauliZ(i) @ qml.PauliZ(i + 1) for i in range(num_qubits - 1)]
[qml.PauliX(i) @ qml.PauliX(i + 1) for i in range(num_qubits - 1)]
+ [qml.PauliY(i) @ qml.PauliY(i + 1) for i in range(num_qubits - 1)]
+ [qml.PauliZ(i) @ qml.PauliZ(i + 1) for i in range(num_qubits - 1)]
)

##############################################################################
Expand Down Expand Up @@ -652,9 +657,10 @@ def circuit(params, **kwargs):

dev_exact = qml.device("default.qubit", wires=num_qubits)
# change the simulator to be the exact one.
circuit.device = dev_exact
circuit = qml.QNode(circuit_base, dev_exact)

expval_exact = [
circuit(params, wires=dev_exact.wires, observable=[o]) for o in list_of_observables
circuit(params, observable=[o]) for o in list_of_observables
]

##############################################################################
Expand Down Expand Up @@ -691,7 +697,7 @@ def circuit(params, **kwargs):
# calculate entanglement witnesses, and even find a way to approximate the von Neumann entropy.
# These applications illustrate the potential power
# of classical shadows for the characterization of quantum systems.


##############################################################################
# References
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ pyscf==2.1.1
cirq-core==0.14.1
cirq-pasqal==0.14.1
openfermionpyscf==0.5 # required by 2 tutorials: tutorial_quantum_chemistry and tutorial_adaptive_circuits and tutorial_diffable_shadows
pennylane-lightning
git+https://github.com/PennyLaneAI/pennylane.git
git+https://github.com/PennyLaneAI/pennylane-sf.git
git+https://github.com/PennyLaneAI/pennylane-cirq.git
Expand Down
1 change: 1 addition & 0 deletions requirements_no_deps.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
mitiq==0.13.0
pyquil==2.21
pennylane-lightning

0 comments on commit e8811cd

Please sign in to comment.