def test_input_gates_not_of_correct_form(self): """Test that an error is thrown if gates marked as encoding gates are not single-parameter gates.""" dev = qml.device("default.qubit", wires=3) @qml.qnode(dev) def circuit(): qml.RX(0.1, wires=0, id="x") qml.Rot(0.2, 0.3, 0.4, wires=1, id="x") return qml.expval(qml.PauliZ(wires=0)) with pytest.raises(ValueError, match="Can only consider one-parameter gates"): spectrum(circuit)()
def test_spectrum_changes_with_qnode_args(self): """Test that the spectrum changes per call if a qnode argument changes the circuit architecture.""" dev = qml.device("default.qubit", wires=3) @qml.qnode(dev) def circuit(last_gate): qml.RX(0.1, wires=0, id="x") qml.RX(0.2, wires=1, id="x") if last_gate: qml.RX(0.3, wires=2, id="x") return qml.expval(qml.PauliZ(wires=0)) res_true = spectrum(circuit)(True) assert np.allclose(res_true["x"], range(-3, 4)) res_false = spectrum(circuit)(False) assert np.allclose(res_false["x"], range(-2, 3))
def test_encoding_gates(self): """Test that the spectrum contains the ids provided in encoding_gates, or all ids if encoding_gates is None.""" dev = qml.device("default.qubit", wires=1) @qml.qnode(dev) def circuit(x): qml.RX(x, wires=0, id="x") qml.RY(0.4, wires=0, id="other") return qml.expval(qml.PauliZ(wires=0)) res = spectrum(circuit, encoding_gates=["x"])(0.1) assert res == {"x": [-1.0, 0.0, 1.0]} res = spectrum(circuit, encoding_gates=["x", "other"])(0.1) assert res == {"x": [-1.0, 0.0, 1.0], "other": [-1.0, 0.0, 1.0]} res = spectrum(circuit)(0.1) assert res == {"x": [-1.0, 0.0, 1.0], "other": [-1.0, 0.0, 1.0]} res = spectrum(circuit, encoding_gates=["a"])(0.1) assert res == {"a": []}
def test_integration_autograd(self): """Test that the spectra of a circuit is calculated correctly in the autograd interface.""" x = pnp.array([1.0, 2.0, 3.0], requires_grad=False) w = pnp.array([[-1, -2, -3], [-4, -5, -6]], requires_grad=True) dev = qml.device("default.qubit", wires=3) qnode = qml.QNode(circuit, dev, interface="autograd") res = spectrum(qnode)(x, w) for (k1, v1), (k2, v2) in zip(res.items(), expected_result.items()): assert k1 == k2 assert v1 == v2
def test_integration_tf(self): """Test that the spectra of a circuit is calculated correctly in the tf interface.""" tf = pytest.importorskip("tensorflow") dev = qml.device("default.qubit", wires=3) qnode = qml.QNode(circuit, dev, interface="tf") x = tf.Variable([1.0, 2.0, 3.0]) w = tf.constant([[-1, -2, -3], [-4, -5, -6]]) res = spectrum(qnode)(x, w) assert res for (k1, v1), (k2, v2) in zip(res.items(), expected_result.items()): assert k1 == k2 assert v1 == v2
def test_integration_torch(self): """Test that the spectra of a circuit is calculated correctly in the torch interface.""" torch = pytest.importorskip("torch") x = torch.tensor([1.0, 2.0, 3.0], requires_grad=True) w = torch.tensor([[-1, -2, -3], [-4, -5, -6]], requires_grad=False) dev = qml.device("default.qubit", wires=3) qnode = qml.QNode(circuit, dev, interface="torch") res = spectrum(qnode)(x, w) assert res for (k1, v1), (k2, v2) in zip(res.items(), expected_result.items()): assert k1 == k2 assert v1 == v2
def test_spectrum_grows_with_gates(self, n_layers, n_qubits): """Test that the spectrum grows linearly with the number of encoding gates if we use Pauli rotation encoding.""" dev = qml.device("default.qubit", wires=n_qubits) @qml.qnode(dev) def circuit(x): for l in range(n_layers): for i in range(n_qubits): qml.RX(x, wires=i, id="x") qml.RY(0.4, wires=i) return qml.expval(qml.PauliZ(wires=0)) res = spectrum(circuit)(0.1) expected_degree = n_qubits * n_layers assert np.allclose(res["x"], range(-expected_degree, expected_degree + 1))
def test_integration_jax(self): """Test that the spectra of a circuit is calculated correctly in the jax interface.""" jax = pytest.importorskip("jax") from jax import numpy as jnp x = jnp.array([1.0, 2.0, 3.0]) w = [[-1, -2, -3], [-4, -5, -6]] dev = qml.device("default.qubit", wires=3) qnode = qml.QNode(circuit, dev, interface="jax") res = spectrum(qnode)(x, w) assert res for (k1, v1), (k2, v2) in zip(res.items(), expected_result.items()): assert k1 == k2 assert v1 == v2