From e8811cdfe6d93b63335f6fe06ebbb02e491f48c1 Mon Sep 17 00:00:00 2001 From: Romain Moyard Date: Thu, 23 Feb 2023 16:32:33 -0500 Subject: [PATCH] Update classical shadows (#708) * More * typo * Trigger CI * lightning req no depth * Trigger CI --- demonstrations/tutorial_classical_shadows.py | 26 ++++++++++++-------- requirements.txt | 1 - requirements_no_deps.txt | 1 + 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/demonstrations/tutorial_classical_shadows.py b/demonstrations/tutorial_classical_shadows.py index b82544f943..80b7b87d3d 100644 --- a/demonstrations/tutorial_classical_shadows.py +++ b/demonstrations/tutorial_classical_shadows.py @@ -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): @@ -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) @@ -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. @@ -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. @@ -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 @@ -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) @@ -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) ############################################################################## @@ -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)] ) ############################################################################## @@ -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 ] ############################################################################## @@ -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 diff --git a/requirements.txt b/requirements.txt index cb793fce5c..3fb1c53526 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 diff --git a/requirements_no_deps.txt b/requirements_no_deps.txt index fd5d3d3aa6..2cbcb376ed 100644 --- a/requirements_no_deps.txt +++ b/requirements_no_deps.txt @@ -1,2 +1,3 @@ mitiq==0.13.0 pyquil==2.21 +pennylane-lightning \ No newline at end of file