Spaces:
Runtime error
Runtime error
Download src/tests/test_circuits.py from QuantumGenAI/ErrorMitigation: direct link, hf CLI and curl.
- Browser
- Download file 5.22 kB
-
https://huggingface.co/spaces/QuantumGenAI/ErrorMitigation/resolve/main/src/tests/test_circuits.py
- Command line
-
hf download hf://spaces/QuantumGenAI/ErrorMitigation/src/tests/test_circuits.py
-
curl -L -o test_circuits.py https://huggingface.co/spaces/QuantumGenAI/ErrorMitigation/resolve/main/src/tests/test_circuits.py
5.22 kB
| from pennylane import numpy as np | |
| from typing import Optional, List, Any, Tuple, Callable | |
| from quantum_circuits.noisy_circuit import NoisyCircuit, print_results | |
| import pennylane as qml | |
| # Test 1: Multiple Noise Channels (BitFlip, Depolarizing, PhaseFlip) | |
| def my_clean_circuit( | |
| params: Optional[Any] = None, n_qubits: Optional[int] = None | |
| ) -> None: | |
| qml.Hadamard(wires=0) | |
| qml.CNOT(wires=[0, 1]) | |
| qml.RX(0.2, wires=0) | |
| noise_ops1: List[Tuple[Callable, float, List[int]]] = [ | |
| (qml.BitFlip, 0.1, [0, 1]), | |
| (qml.DepolarizingChannel, 0.05, [0]), | |
| (qml.PhaseFlip, 0.2, [1]), | |
| ] | |
| # Device with enough wires (2 system + ancillas) | |
| dev_naive1 = qml.device("default.mixed", wires=7, shots=None) | |
| nc1 = NoisyCircuit(device=dev_naive1, noise_operations=noise_ops1) | |
| qnode_stine1 = nc1.noisy_qnode(circuit=my_clean_circuit, wires=[0, 1], use_qasm=True) | |
| qnode_naive1 = nc1.noisy_qnode(circuit=my_clean_circuit, wires=[0, 1], use_qasm=False) | |
| res_stine1 = np.round(qnode_stine1(), 3) | |
| res_naive1 = np.round(qnode_naive1(), 3) | |
| print_results( | |
| "Test 1: Multiple Noise Channels (BitFlip, Depolarizing, PhaseFlip)", | |
| res_stine1, | |
| res_naive1, | |
| qnode_stine1, | |
| qnode_naive1, | |
| ) | |
| ################################################################## | |
| # Test 2: Single Noise Channel (AmplitudeDamping) | |
| ################################################################## | |
| def my_clean_circuit2(params=None, n_qubits=None): | |
| qml.Hadamard(wires=0) | |
| qml.CNOT(wires=[0, 1]) | |
| noise_ops2 = [(qml.AmplitudeDamping, 0.3, [0, 1])] | |
| dev_naive2 = qml.device("default.mixed", wires=4, shots=None) | |
| nc2 = NoisyCircuit(device=dev_naive2, noise_operations=noise_ops2) | |
| qnode_stine2 = nc2.noisy_qnode(circuit=my_clean_circuit2, wires=[0, 1], use_qasm=True) | |
| qnode_naive2 = nc2.noisy_qnode(circuit=my_clean_circuit2, wires=[0, 1], use_qasm=False) | |
| res_stine2 = np.round(qnode_stine2(), 3) | |
| res_naive2 = np.round(qnode_naive2(), 3) | |
| print_results( | |
| "Test 2: Single Noise Channel (AmplitudeDamping = 0.3)", | |
| res_stine2, | |
| res_naive2, | |
| qnode_stine2, | |
| qnode_naive2, | |
| ) | |
| ################################################################## | |
| # Test 3: No Noise Channels (Noiseless Circuit) | |
| ################################################################## | |
| def my_clean_circuit3(params=None, n_qubits=None): | |
| qml.Hadamard(wires=0) | |
| qml.CNOT(wires=[0, 1]) | |
| qml.RY(0.5, wires=1) | |
| noise_ops3 = [] # No noise | |
| dev_naive3 = qml.device("default.mixed", wires=2, shots=None) | |
| nc3 = NoisyCircuit(device=dev_naive3, noise_operations=noise_ops3) | |
| qnode_stine3 = nc3.noisy_qnode(circuit=my_clean_circuit3, wires=[0, 1], use_qasm=True) | |
| qnode_naive3 = nc3.noisy_qnode(circuit=my_clean_circuit3, wires=[0, 1], use_qasm=False) | |
| res_stine3 = np.round(qnode_stine3(), 3) | |
| res_naive3 = np.round(qnode_naive3(), 3) | |
| print_results( | |
| "Test 3: No Noise Channels (Noiseless Circuit)", | |
| res_stine3, | |
| res_naive3, | |
| qnode_stine3, | |
| qnode_naive3, | |
| ) | |
| ################################################################## | |
| # Test 4: All Noise Channels Applied Together | |
| ################################################################## | |
| def my_clean_circuit4(params=None, n_qubits=None): | |
| qml.Hadamard(wires=0) | |
| qml.SWAP(wires=[0, 1]) | |
| qml.RZ(0.3, wires=1) | |
| noise_ops4 = [ | |
| (qml.BitFlip, 0.05, [0]), | |
| (qml.PhaseFlip, 0.05, [1]), | |
| (qml.DepolarizingChannel, 0.05, [0, 1]), | |
| (qml.AmplitudeDamping, 0.1, [0]), | |
| (qml.PhaseDamping, 0.1, [1]), | |
| ] | |
| # Total wires: 2 system wires + (1 + 1 + 2 + 1 + 1) = 7 wires. | |
| dev_naive4 = qml.device("default.mixed", wires=7, shots=None) | |
| nc4 = NoisyCircuit(device=dev_naive4, noise_operations=noise_ops4) | |
| qnode_stine4 = nc4.noisy_qnode(circuit=my_clean_circuit4, wires=[0, 1], use_qasm=True) | |
| qnode_naive4 = nc4.noisy_qnode(circuit=my_clean_circuit4, wires=[0, 1], use_qasm=False) | |
| res_stine4 = np.round(qnode_stine4(), 3) | |
| res_naive4 = np.round(qnode_naive4(), 3) | |
| print_results( | |
| "Test 4: All Noise Channels Applied Together", | |
| res_stine4, | |
| res_naive4, | |
| qnode_stine4, | |
| qnode_naive4, | |
| ) | |
| ################################################################## | |
| # Test 5: Multiple Noise Channels with Varying Probabilities | |
| ################################################################## | |
| def my_clean_circuit5(params=None, n_qubits=None): | |
| qml.Hadamard(wires=0) | |
| qml.CNOT(wires=[0, 1]) | |
| qml.CZ(wires=[1, 2]) | |
| qml.RY(0.4, wires=2) | |
| noise_ops5 = [ | |
| (qml.BitFlip, 0.2, [0, 1]), | |
| (qml.PhaseFlip, 0.15, [1, 2]), | |
| (qml.DepolarizingChannel, 0.1, [0, 2]), | |
| (qml.AmplitudeDamping, 0.25, [0]), | |
| (qml.PhaseDamping, 0.2, [2]), | |
| ] | |
| # Total wires: 3 system wires + (2 + 2 + 2 + 1 + 1) = 11 wires. | |
| dev_naive5 = qml.device("default.mixed", wires=11, shots=None) | |
| nc5 = NoisyCircuit(device=dev_naive5, noise_operations=noise_ops5) | |
| qnode_stine5 = nc5.noisy_qnode( | |
| circuit=my_clean_circuit5, wires=[0, 1, 2], use_qasm=True | |
| ) | |
| qnode_naive5 = nc5.noisy_qnode( | |
| circuit=my_clean_circuit5, wires=[0, 1, 2], use_qasm=False | |
| ) | |
| res_stine5 = np.round(qnode_stine5(), 3) | |
| res_naive5 = np.round(qnode_naive5(), 3) | |
| print_results( | |
| "Test 5: Multiple Noise Channels with Varying Probabilities", | |
| res_stine5, | |
| res_naive5, | |
| qnode_stine5, | |
| qnode_naive5, | |
| ) | |