Exemplo n.º 1
0
def test_about(capfd):
    """sf.about works."""
    sf.about()
    out, err = capfd.readouterr()
    # substantial output (actual length varies)
    assert len(err) == 0
    assert len(out) > 300

    assert "Strawberry Fields version" in out
    sf_version_match = re.search(r"Strawberry Fields version:\s+([\S]+)\n",
                                 out).group(1)
    assert sf_version_match == sf.version()

    assert "Numpy version" in out
    assert "Scipy version" in out
    assert "Hafnian version" in out
    assert "Blackbird version" in out
Exemplo n.º 2
0
    def test_power_positive_frac(self):
        a = np.random.random()
        x = np.random.random()
        q = self.eng.register
        rrt = power(q[0], a)

        self.assertEqual(type(rrt), sf.engine.RegRefTransform)
        self.assertEqual(rrt.func(x), x**a)

    def test_power_negative_int(self):
        a = -3
        x = np.random.random()
        q = self.eng.register
        rrt = power(q[0], a)

        self.assertEqual(type(rrt), sf.engine.RegRefTransform)
        self.assertEqual(rrt.func(x), x**a)


if __name__ == '__main__':
    print('Testing Strawberry Fields version ' + sf.version() + ', Utils.')

    # run the tests in this file
    suite = unittest.TestSuite()
    tests = [InitialStates, FockInitialStates, ConvertFunctions]
    for t in tests:
        ttt = unittest.TestLoader().loadTestsFromTestCase(t)
        suite.addTests(ttt)

    unittest.TextTestRunner().run(suite)
    def test_optimization(self):
        filename = os.path.join(examples_folder,"optimization.py")
        soln = b"Value at step 49: 0.36787864565849304\n"
        # tests whether the output fock probability are as
        # expected, as written in soln
        p = Popen(["python3", filename], stdout=PIPE, 
                       preexec_fn=os.setsid)

        out = p.stdout.readlines()[-1]
        self.assertEqual(out, soln)

        p.terminate()


if __name__ == '__main__':
    print('Testing Strawberry Fields version ' + sf.version() + ', examples.')

    # run the tests in this file
    suite = unittest.TestSuite()
    tests = [
        GaussianExamples,
        FockExamples,
        TFExamples
    ]
    for t in tests:
        ttt = unittest.TestLoader().loadTestsFromTestCase(t)
        suite.addTests(ttt)

    unittest.TextTestRunner().run(suite)