def test_pre_measure_eng(self, tol): """Test that the pre_measure method operates as expected by initializing the engine correctly""" dev = qml.device("strawberryfields.gbs", wires=4, cutoff_dim=3) prog = Program(4) op1 = GraphEmbed(0.1767767 * np.ones((4, 4)), mean_photon_per_mode=0.25) prog.append(op1, prog.register) dev.prog = prog dev.pre_measure() assert dev.eng.backend_name == "gaussian" assert dev.eng.backend_options == {"cutoff_dim": 3}
def test_pre_measure_state_and_samples(self, tol): """Test that the pre_measure method operates as expected in analytic mode by generating the correct output state and not generating samples""" dev = qml.device("strawberryfields.gbs", wires=4, cutoff_dim=3) prog = Program(4) op1 = GraphEmbed(0.1767767 * np.ones((4, 4)), mean_photon_per_mode=0.25) prog.append(op1, prog.register) dev.prog = prog dev.pre_measure() assert np.allclose(dev.state.displacement(), np.zeros(4)) assert np.allclose(dev.state.cov(), target_cov, atol=tol) assert dev.samples.size == 0
def apply(self, operation, wires, par): self._params, A, n_mean = par A *= rescale(A, n_mean) self.Z_inv = self._calculate_z_inv(A) if len(self._params) != self.num_wires: raise ValueError( "The number of variable parameters must be equal to the total number of wires." ) self._WAW = self.calculate_WAW(self._params, A) if self.shots is not None and self._use_cache: op = GraphEmbed(A, mean_photon_per_mode=n_mean / len(A)) else: n_mean_WAW = self.calculate_n_mean(self._WAW) op = GraphEmbed(self._WAW, mean_photon_per_mode=n_mean_WAW / len(A)) op | [self.q[wires.index(i)] for i in wires] if self.shots is not None: MeasureFock() | [self.q[wires.index(i)] for i in wires]
def test_pre_measure_state_and_samples_non_analytic(self, tol): """Test that the pre_measure method operates as expected in non-analytic mode by generating the correct output state and samples of the right shape""" dev = qml.device("strawberryfields.gbs", wires=4, cutoff_dim=3, analytic=False, shots=2) prog = Program(4) op1 = GraphEmbed(0.1767767 * np.ones((4, 4)), mean_photon_per_mode=0.25) op2 = MeasureFock() prog.append(op1, prog.register) prog.append(op2, prog.register) dev.prog = prog dev.pre_measure() assert np.allclose(dev.state.displacement(), np.zeros(4)) assert np.allclose(dev.state.cov(), target_cov, atol=tol) assert dev.samples.shape == (2, 4)
def test_pre_measure_non_analytic_cache(self, mocker): """Test that the pre_measure method does not overwrite existing samples if present in non-analytic mode when use_cache is ``True``""" samples = -1 * np.ones((10, 4)) dev = qml.device( "strawberryfields.gbs", wires=4, cutoff_dim=3, shots=10, samples=samples, use_cache=True, ) prog = Program(4) op1 = GraphEmbed(0.1767767 * np.ones((4, 4)), mean_photon_per_mode=0.25) op2 = MeasureFock() prog.append(op1, prog.register) prog.append(op2, prog.register) dev.prog = prog dev.pre_measure() spy = mocker.spy(sf.Engine, "run") assert np.allclose(dev.samples, samples) spy.assert_not_called()