示例#1
0
    def test_squeezing(self):
        """Test squeezing gives correct means and cov"""
        self.eng.reset()
        q = self.eng.register

        x = 0.2
        p = 0.3
        r = 0.42
        phi = 0.123
        H, t = squeezing(r, phi, hbar=self.hbar)

        with self.eng:
            Xgate(x) | q[0]
            Zgate(p) | q[0]
            GaussianPropagation(H, t) | q[0]

        state = self.eng.run('gaussian')

        # test the covariance matrix
        res = state.cov()
        S = rot(phi / 2) @ np.diag(np.exp([-r, r])) @ rot(phi / 2).T
        V = S @ S.T * self.hbar / 2
        self.assertTrue(np.allclose(res, V))

        # test the vector of means
        res = state.means()
        exp = S @ np.array([x, p])
        self.assertTrue(np.allclose(res, exp))
示例#2
0
    def test_rotation(self):
        """Test rotation gives correct means and cov"""
        self.eng.reset()
        q = self.eng.register

        x = 0.2
        p = 0.3
        phi = 0.123
        H, t = rotation(phi, hbar=self.hbar)

        with self.eng:
            Xgate(x) | q[0]
            Zgate(p) | q[0]
            Sgate(2) | q[0]
            GaussianPropagation(H, t) | q[0]

        state = self.eng.run('gaussian')

        # test the covariance matrix
        res = state.cov()
        V = np.diag([np.exp(-4), np.exp(4)]) * self.hbar / 2
        expected = rot(phi) @ V @ rot(phi).T
        self.assertTrue(np.allclose(res, expected))

        # test the vector of means
        res = state.means()
        exp = rot(phi) @ np.diag(np.exp([-2, 2])) @ np.array([x, p])
        self.assertTrue(np.allclose(res, exp))
示例#3
0
    def test_quadratic_phase(self):
        """Test quadratic phase gives correct means and cov"""
        self.eng.reset()
        q = self.eng.register

        x = 0.2
        p = 0.3
        s = 0.432
        H, t = quadratic_phase(s)

        with self.eng:
            Xgate(x) | q[0]
            Zgate(p) | q[0]
            GaussianPropagation(H, t) | q[0]

        state = self.eng.run('gaussian')

        # test the covariance matrix
        res = state.cov()
        expected = np.array([[1, s], [s, 1 + s**2]]) * self.hbar / 2
        self.assertTrue(np.allclose(res, expected))

        # test the vector of means
        res = state.means()
        expected = np.array([x, p + s * x])
        self.assertTrue(np.allclose(res, expected))
示例#4
0
    def test_displaced_oscillator(self):
        """Test that a forced quantum oscillator produces the correct
        self.logTestName()
        trajectory in the phase space"""
        H = QuadOperator('q0 q0', 0.5)
        H += QuadOperator('p0 p0', 0.5)
        H -= QuadOperator('q0', self.F)

        res = []
        tlist = np.arange(0, self.t, self.dt)

        for t in tlist:  #pylint: disable=unused-variable
            self.eng.reset()
            q = self.eng.register
            with self.eng:
                Xgate(self.x0) | q[0]
                Zgate(self.p0) | q[0]
                GaussianPropagation(H, self.t) | q

            state = self.eng.run('gaussian')
            res.append(state.means().tolist())

        res = np.array(res)[-1]
        expected = self.displaced_oscillator_soln(self.t)
        self.assertTrue(np.allclose(res, expected))
    def test_rotation(self, hbar):
        """Test rotation gives correct means and cov"""
        prog = sf.Program(1)
        eng = sf.Engine("gaussian")

        x = 0.2
        p = 0.3
        phi = 0.123
        H, t = rotation(phi, hbar=hbar)

        with prog.context as q:
            Xgate(x) | q[0]
            Zgate(p) | q[0]
            Sgate(2) | q[0]
            GaussianPropagation(H, t) | q[0]

        state = eng.run(prog).state

        # test the covariance matrix
        res = state.cov()
        V = np.diag([np.exp(-4), np.exp(4)]) * hbar / 2
        expected = rot(phi) @ V @ rot(phi).T
        assert np.allclose(res, expected)

        # test the vector of means
        res = state.means()
        exp = rot(phi) @ np.diag(np.exp([-2, 2])) @ np.array([x, p])
        assert np.allclose(res, exp)
    def test_squeezing(self, hbar):
        """Test squeezing gives correct means and cov"""
        prog = sf.Program(1)
        eng = sf.Engine("gaussian")

        x = 0.2
        p = 0.3
        r = 0.42
        phi = 0.123
        H, t = squeezing(r, phi, hbar=hbar)

        with prog.context as q:
            Xgate(x) | q[0]
            Zgate(p) | q[0]
            GaussianPropagation(H, t) | q[0]

        state = eng.run(prog).state

        # test the covariance matrix
        res = state.cov()
        S = rot(phi / 2) @ np.diag(np.exp([-r, r])) @ rot(phi / 2).T
        V = S @ S.T * hbar / 2
        assert np.allclose(res, V)

        # test the vector of means
        res = state.means()
        exp = S @ np.array([x, p])
        assert np.allclose(res, exp)
    def test_displaced_oscillator(self):
        """Test that a forced quantum oscillator produces the correct
        self.logTestName()
        trajectory in the phase space"""
        H = QuadOperator('q0 q0', 0.5)
        H += QuadOperator('p0 p0', 0.5)
        H -= QuadOperator('q0', self.F)

        res = []
        tlist = np.arange(0, self.t, self.dt)

        eng = sf.Engine("gaussian")

        for idx, t in enumerate(tlist):  #pylint: disable=unused-variable
            prog = sf.Program(1)

            with prog.context as q:
                Xgate(self.x0) | q[0]
                Zgate(self.p0) | q[0]
                GaussianPropagation(H, self.t) | q

            state = eng.run(prog).state
            eng.reset()
            res.append(state.means().tolist())

        res = np.array(res)[-1]
        expected = self.displaced_oscillator_soln(self.t)
        assert np.allclose(res, expected)
    def test_quadratic_phase(self, hbar):
        """Test quadratic phase gives correct means and cov"""
        prog = sf.Program(1)
        eng = sf.Engine("gaussian")

        x = 0.2
        p = 0.3
        s = 0.432
        H, t = quadratic_phase(s)

        with prog.context as q:
            Xgate(x) | q[0]
            Zgate(p) | q[0]
            GaussianPropagation(H, t) | q[0]

        state = eng.run(prog).state

        # test the covariance matrix
        res = state.cov()
        expected = np.array([[1, s], [s, 1 + s**2]]) * hbar / 2
        assert np.allclose(res, expected)

        # test the vector of means
        res = state.means()
        expected = np.array([x, p + s * x])
        assert np.allclose(res, expected)
示例#9
0
def init_layer(q):
    """Create an initial state with defined
    squeezing and displacement"""
    Xgate(0.2) | q[0]
    Xgate(0.4) | q[1]
    Zgate(0.3) | q[1]
    Sgate(0.1) | q[0]
    Sgate(-0.1) | q[1]
    return q
示例#10
0
 def decompose(self, reg):
     """Return the decomposed commands"""
     cmds = []
     cmds += [Command(GaussianTransform(self.S), reg)]
     if self.disp:
         cmds += [
             Command(Xgate(x), reg) for x in self.d[:self.ns] if x != 0.
         ]
         cmds += [
             Command(Zgate(z), reg) for z in self.d[self.ns:] if z != 0.
         ]
     return cmds
示例#11
0
r = np.abs(alpha)
phi = np.angle(alpha)

with prog.context as q:
    # prepare initial states
    Coherent(r, phi) | q[0]
    Squeezed(-2) | q[1]
    Squeezed(2) | q[2]

    # apply gates
    BS = BSgate(pi / 4, 0)
    BS | (q[1], q[2])
    BS | (q[0], q[1])

    # Perform homodyne measurements
    MeasureX | q[0]
    MeasureP | q[1]

    # Displacement gates conditioned on
    # the measurements
    Xgate(sqrt(2) * q[0].par) | q[2]
    Zgate(sqrt(2) * q[1].par) | q[2]

eng = sf.Engine("fock", backend_options={"cutoff_dim": 15})
result = eng.run(prog)

# view output state and fidelity
print(result.samples)
print(result.state)
print(result.state.fidelity_coherent([0, 0, alpha]))