def test_tensorn_one_mode_is_mean_photon(self, tol): """Test variance of TensorN for a single mode, which resorts to calculations for the NumberOperator""" dev = qml.device("strawberryfields.gaussian", wires=1) op = qml.TensorN(wires=[0]) # Check that instantiating TensorN on one mode returns the # NumberOperator assert isinstance(op, qml.NumberOperator) @qml.qnode(dev) def circuit(n, a): qml.ThermalState(n, wires=0) qml.Displacement(a, 0, wires=0) return qml.var(op) n = 0.12 a = 0.765 var = circuit(n, a) expected = n**2 + n + np.abs(a)**2 * (1 + 2 * n) assert np.allclose(var, expected, atol=tol, rtol=0) # circuit jacobians gradF = circuit.jacobian([n, a], method="F") expected = np.array([2 * a**2 + 2 * n + 1, 2 * a * (2 * n + 1)]) assert np.allclose(gradF, expected, atol=tol, rtol=0)
def test_tensorn_one_mode_is_mean_photon(self, tol): """Test variance of TensorN for a single mode, which resorts to calculations for the NumberOperator""" dev = qml.device("strawberryfields.fock", wires=1, cutoff_dim=15) op = qml.TensorN(wires=[0]) # Check that instantiating TensorN on one mode returns the # NumberOperator assert isinstance(op, qml.NumberOperator) @qml.qnode(dev) def circuit(n, a): qml.ThermalState(n, wires=0) qml.Displacement(a, 0, wires=0) return qml.var(op) n = np.array(0.12, requires_grad=True) a = np.array(0.105, requires_grad=True) var = circuit(n, a) expected = n**2 + n + np.abs(a) ** 2 * (1 + 2 * n) assert np.allclose(var, expected, atol=tol, rtol=0) # circuit jacobians tapes, fn = qml.gradients.finite_diff(circuit.qtape) gradF = fn(dev.batch_execute(tapes)).flatten() expected = np.array([2 * a**2 + 2 * n + 1, 2 * a * (2 * n + 1)]) assert np.allclose(gradF, expected, atol=tol, rtol=0)
def test_gradient_displaced_thermal_var_photon(self, tol): """Test gradient of the photon variance of a displaced thermal state""" dev = qml.device("strawberryfields.tf", wires=1, cutoff_dim=15) op = qml.TensorN(wires=[0]) # Check that instantiating TensorN on one mode returns the # NumberOperator assert isinstance(op, qml.NumberOperator) @qml.qnode(dev, interface="tf", diff_method="backprop") def circuit(n, a): qml.ThermalState(n, wires=0) qml.Displacement(a, 0, wires=0) return qml.var(op) n = tf.Variable(0.12) a = tf.Variable(0.105) with tf.GradientTape(persistent=True) as tape: var = circuit(n, a) # circuit jacobians grad = tape.gradient(var, [n, a]) expected = np.array([2 * a**2 + 2 * n + 1, 2 * a * (2 * n + 1)]) assert np.allclose(grad, expected, atol=tol, rtol=0)
def test_tensor_n_single_mode_wires_implicit(self): """Checks that instantiating TensorN when passing a single mode as a positional argument returns a NumberOperator.""" cv_obs = qml.TensorN(1) assert isinstance(cv_obs, qml.NumberOperator) assert cv_obs.wires == Wires([1]) assert cv_obs.ev_order == 2
def test_tensor_n_single_mode_wires_explicit(self): """Checks that instantiating a TensorN when passing a single mode as a keyword argument returns a NumberOperator.""" cv_obs = qml.TensorN(wires=[0]) assert isinstance(cv_obs, qml.NumberOperator) assert cv_obs.wires == Wires([0]) assert cv_obs.ev_order == 2
def test_tensor_n_multiple_modes(self): """Checks that the TensorN operator was constructed correctly when multiple modes were specified.""" cv_obs = qml.TensorN(wires=[0, 1]) assert isinstance(cv_obs, qml.TensorN) assert cv_obs.wires == Wires([0, 1]) assert cv_obs.ev_order is None
def test_displaced_thermal_mean_photon_variance(self, tol): """Test gradient of the photon variance of a displaced thermal state""" dev = qml.device("default.gaussian", wires=1) n = 0.12 a = 0.105 with CVParamShiftTape() as tape: qml.ThermalState(n, wires=0) qml.Displacement(a, 0, wires=0) var(qml.TensorN(wires=[0])) tape.trainable_params = {0, 1} grad = tape.jacobian(dev) expected = np.array([2 * a ** 2 + 2 * n + 1, 2 * a * (2 * n + 1)]) assert np.allclose(grad, expected, atol=tol, rtol=0)
def test_displaced_thermal_mean_photon_variance(self, tol): """Test gradient of the photon variance of a displaced thermal state""" dev = qml.device("default.gaussian", wires=1) n = 0.12 a = 0.105 with qml.tape.JacobianTape() as tape: qml.ThermalState(n, wires=0) qml.Displacement(a, 0, wires=0) qml.var(qml.TensorN(wires=[0])) tape.trainable_params = {0, 1} tapes, fn = param_shift_cv(tape, dev) grad = fn(dev.batch_execute(tapes)) expected = np.array([2 * a**2 + 2 * n + 1, 2 * a * (2 * n + 1)]) assert np.allclose(grad, expected, atol=tol, rtol=0)
def circuit(theta, phi): qml.Beamsplitter(theta, phi, wires=[wires[0], wires[1]]) qml.Beamsplitter(theta, phi, wires=[wires[4], wires[5]]) return qml.expval(qml.TensorN(wires=wires))
def circuit(pars): qml.Squeezing(pars[0], pars[1], wires=0) qml.Displacement(pars[2], pars[3], wires=0) qml.Squeezing(pars[4], pars[5], wires=1) qml.Displacement(pars[6], pars[7], wires=1) return qml.var(qml.TensorN(wires=[0, 1]))
def circuit(a, phi): qml.Displacement(a, phi, wires=0) qml.Displacement(a, phi, wires=1) return qml.var(qml.TensorN(wires=[0, 1]))