コード例 #1
0
ファイル: tensor_math.py プロジェクト: xbe/qcc
def hipster_multi():
    """Multi-qubit, optimized application."""

    nbits = 7
    for bits in helper.bitprod(nbits):
        psi = state.bitstring(*bits)
        for target in range(1, nbits):
            # Full matrix (O(n*n).
            op = (ops.Identity(target - 1) * ops.Cnot(target - 1, target) *
                  ops.Identity(nbits - target - 1))
            psi1 = op(psi)

            # Single Qubit (O(n))
            psi = apply_controlled_gate(ops.PauliX(), target - 1, target, psi)
            if not psi.is_close(psi1):
                raise AssertionError('Invalid Single Gate Application.')

    psi = state.bitstring(1, 1, 0, 0, 1)
    pn = ops.Cnot(1, 4)(psi, 1)
    if not pn.is_close(apply_controlled_gate(ops.PauliX(), 1, 4, psi)):
        raise AssertionError('Invalid Cnot')
    pn = ops.Cnot(4, 1)(psi, 1)
    if not pn.is_close(apply_controlled_gate(ops.PauliX(), 4, 1, psi)):
        raise AssertionError('Invalid Cnot')
    pn = ops.ControlledU(0, 1, ops.ControlledU(1, 4, ops.PauliX()))(psi)

    psi = state.qubit(alpha=0.6) * state.ones(2)
    pn = ops.Cnot(0, 2)(psi)
    if not pn.is_close(apply_controlled_gate(ops.PauliX(), 0, 2, psi)):
        raise AssertionError('Invalid Cnot')
コード例 #2
0
ファイル: state_test.py プロジェクト: qcc4cp/qcc
 def test_density(self):
     psi = state.bitstring(1, 0)
     rho = psi.density()
     self.assertTrue(rho.is_density())
     self.assertTrue(rho.is_hermitian())
     self.assertTrue(rho.is_pure())
     self.assertFalse(rho.is_unitary())
コード例 #3
0
 def test_dft_adjoint(self):
   bits = [0, 1, 0, 1, 1, 0]
   psi = state.bitstring(*bits)
   psi = ops.Qft(6)(psi)
   psi = ops.Qft(6).adjoint()(psi)
   maxbits, _ = psi.maxprob()
   self.assertEqual(maxbits, tuple(bits))
コード例 #4
0
def run_experiment(a1: np.complexfloating, a2: np.complexfloating,
                   target: float) -> None:
    """Construct swap test circuit and measure."""

    # The circuit is quite simple:
    #
    # |0> --- H --- o --- H --- Measure
    #               |
    # a1  --------- x ---------
    #               |
    # a2  ----------x ---------

    psi = state.bitstring(0) * state.qubit(a1) * state.qubit(a2)
    psi = ops.Hadamard()(psi, 0)
    psi = ops.ControlledU(0, 1, ops.Swap(1, 2))(psi)
    psi = ops.Hadamard()(psi, 0)

    # Measure once.
    p0, _ = ops.Measure(psi, 0)
    if abs(p0 - target) > 0.05:
        raise AssertionError(
            'Probability {:.2f} off more than 5% from target {:.2f}'.format(
                p0, target))
    print('Similarity of a1: {:.2f}, a2: {:.2f} ==>  %: {:.2f}'.format(
        a1, a2, 100.0 * p0))
コード例 #5
0
  def test_cnot0(self):
    """Check implementation of ControlledU via Cnot0."""

    # Check operator itself.
    x = ops.PauliX() * ops.Identity()
    self.assertTrue(ops.Cnot0(0, 1).
                    is_close(x @ ops.Cnot(0, 1) @ x))

    # Compute simplest case with Cnot0.
    psi = state.bitstring(1, 0)
    psi2 = ops.Cnot0(0, 1)(psi)
    self.assertTrue(psi.is_close(psi2))

    # Compute via explicit constrution.
    psi2 = (x @ ops.Cnot(0, 1) @ x)(psi)
    self.assertTrue(psi.is_close(psi2))

    # Different offsets.
    psi2 = ops.Cnot0(0, 1)(state.bitstring(0, 1))
    self.assertTrue(psi2.is_close(state.bitstring(0, 0)))

    psi2 = ops.Cnot0(0, 3)(state.bitstring(0, 0, 0, 0, 1))
    self.assertTrue(psi2.is_close(state.bitstring(0, 0, 0, 1, 1)))

    psi2 = ops.Cnot0(4, 0)(state.bitstring(1, 0, 0, 0, 0))
    self.assertTrue(psi2.is_close(state.bitstring(0, 0, 0, 0, 0)))
コード例 #6
0
ファイル: state_test.py プロジェクト: qcc4cp/qcc
    def test_schmidt(self):
        psi = state.zeros(2)
        self.assertEqual(psi.schmidt_number([1]), 1.0)

        psi = state.bitstring(0, 1, 1, 0, 1, 0, 1, 1)
        self.assertEqual(psi.schmidt_number([1]), 1.0)

        psi = state.State(np.array([1.0, 1.0, 0.0, 1.0]))
        self.assertNotEqual(psi.schmidt_number([1]), 1.0)
コード例 #7
0
ファイル: state_test.py プロジェクト: qcc4cp/qcc
    def test_ordering(self):
        a = state.Reg(3, [0, 0, 0], 0)
        self.assertGreater(a.psi()[0], 0.99)
        a = state.Reg(3, [0, 0, 1], 3)
        self.assertGreater(a.psi()[1], 0.99)
        a = state.Reg(3, [1, 1, 0], 6)
        self.assertGreater(a.psi()[6], 0.99)
        a = state.Reg(3, [1, 1, 1], 9)
        self.assertGreater(a.psi()[7], 0.99)

        psi = state.bitstring(0, 0, 0)
        self.assertGreater(psi[0], 0.99)
        psi = state.bitstring(0, 0, 1)
        self.assertGreater(psi[1], 0.99)
        psi = state.bitstring(1, 1, 0)
        self.assertGreater(psi[6], 0.99)
        psi = state.bitstring(1, 1, 1)
        self.assertGreater(psi[7], 0.99)
コード例 #8
0
ファイル: bell.py プロジェクト: xbe/qcc
def bell_state(a, b) -> state.State:
    """Make one of the four bell states with a, b from {0,1}."""

    if a not in [0, 1] or b not in [0, 1]:
        raise ValueError('Bell state arguments are bits and must be 0 or 1.')

    psi = state.bitstring(a, b)
    psi = ops.Hadamard()(psi)
    return ops.Cnot()(psi)
コード例 #9
0
ファイル: state_test.py プロジェクト: qcc4cp/qcc
 def test_probabilities(self):
     psi = state.bitstring(0, 1, 1)
     self.assertEqual(psi.prob(0, 0, 0), 0.0)
     self.assertEqual(psi.prob(0, 0, 1), 0.0)
     self.assertEqual(psi.prob(0, 1, 0), 0.0)
     self.assertEqual(psi.prob(0, 1, 1), 1.0)
     self.assertEqual(psi.prob(1, 0, 0), 0.0)
     self.assertEqual(psi.prob(1, 0, 1), 0.0)
     self.assertEqual(psi.prob(1, 1, 0), 0.0)
     self.assertEqual(psi.prob(1, 1, 1), 0.0)
コード例 #10
0
def experiment_matrix(a, b, cin, expected_sum, expected_cout):
  """Run a simple classic experiment, check results."""

  psi = state.bitstring(a, b, cin, 0, 0)
  psi = fulladder_matrix(psi)

  bsum, _ = ops.Measure(psi, 3, tostate=1, collapse=False)
  bout, _ = ops.Measure(psi, 4, tostate=1, collapse=False)
  print(f'a: {a} b: {b} cin: {cin} sum: {bsum} cout: {bout}')
  if bsum != expected_sum or bout != expected_cout:
    raise AssertionError('invalid results')
コード例 #11
0
def basis_kick2():
  """Another way to look at this H-Cnot-H phase kick."""

  # This produces the vector [0, 1, 0, 0]
  psi = state.bitstring(0, 1)

  # Applying Hadamard: [0.5, -0.5, 0.5, -0.5]
  h2 = ops.Hadamard(2)
  psi = h2(psi)

  # Acting Cnot on this vector: [0.5, -0.5, -0.5, 0.5]
  psi = ops.Cnot()(psi)

  # Final Hadamard: [0, 0, 0, 1]
  psi = h2(psi)

  # which is |11>
  p11 = state.bitstring(1, 1)
  if not psi.is_close(p11):
    raise AssertionError('Something is wrong with the phase kick')
コード例 #12
0
    def test_acceleration(self):
        psi = state.bitstring(1, 0, 1, 0)
        qc = circuit.qc()
        qc.bitstring(1, 0, 1, 0)

        for i in range(4):
            qc.x(i)
            psi.apply(ops.PauliX(), i)
            qc.y(i)
            psi.apply(ops.PauliY(), i)
            qc.z(i)
            psi.apply(ops.PauliZ(), i)
            qc.h(i)
            psi.apply(ops.Hadamard(), i)
            if i:
                qc.cu1(0, i, 1.1)
                psi.apply_controlled(ops.U1(1.1), 0, i)

        if not psi.is_close(qc.psi):
            raise AssertionError('Numerical Problems')

        psi = state.bitstring(1, 0, 1, 0, 1)
        qc = circuit.qc()
        qc.bitstring(1, 0, 1, 0, 1)

        for n in range(5):
            qc.h(n)
            psi.apply(ops.Hadamard(), n)
            for i in range(0, 5):
                qc.cu1(n - (i + 1), n, math.pi / float(2**(i + 1)))
                psi.apply_controlled(ops.U1(math.pi / float(2**(i + 1))),
                                     n - (i + 1), n)
            for i in range(0, 5):
                qc.cu1(n - (i + 1), n, -math.pi / float(2**(i + 1)))
                psi.apply_controlled(ops.U1(-math.pi / float(2**(i + 1))),
                                     n - (i + 1), n)
            qc.h(n)
            psi.apply(ops.Hadamard(), n)

        if not psi.is_close(qc.psi):
            raise AssertionError('Numerical Problems')
コード例 #13
0
    def test_bloch_coords(self):
        psi = state.bitstring(1, 1)
        psi = ops.Qft(2)(psi)

        rho0 = ops.TraceOut(psi.density(), [1])
        rho1 = ops.TraceOut(psi.density(), [0])

        x0, _, _ = helper.density_to_cartesian(rho0)
        _, y1, _ = helper.density_to_cartesian(rho1)

        self.assertTrue(math.isclose(-1.0, x0, abs_tol=0.01))
        self.assertTrue(math.isclose(-1.0, y1, abs_tol=0.01))
コード例 #14
0
ファイル: tensor_math.py プロジェクト: xbe/qcc
def hipster_single():
    """Single-qubit Hipster Technique."""

    # This is a nice trick, outlined in this paper on "Hipster":
    #    https://arxiv.org/pdf/1601.07195.pdf
    #
    # The observation is that to apply a single-qubit gate to a
    # gubit with index i, take the binary representation of inidices and
    # apply the transformation matrix to the elements according
    # to the power of 2 index. Generally:
    # "Performing a single-qubit gate on qubit k of n-qubit quantum
    # register applies G to pairs of amplitudes whose indices differ in
    # k-th bits of their binary index".
    #
    # For example, for a 2-qubit system, to apply a gate to qubit 0:
    #    apply G to
    #    q11, q12   psi[0], psi[1]
    #    q21, q22   psi[2], psi[3]
    #
    # To apply to qubit 1:
    #    q11, q12   psi[0], psi[2]
    #    q21, q22   psi[1], psi[3]
    #
    # 'Outer loop' jumps by 2**(nbits+1)
    # 'Inner loop' jumps by 2**k
    #
    # To maintain the qubit / index ordering of this infrastructure,
    # the qubit index in the paper is reversed to the qubit index here.
    # (Hence the (nbits - qubit - 1) above)
    #

    # Make sure that for sample gates and all states the transformations
    # are identical.
    #
    for gate in (ops.PauliX(), ops.PauliZ(), ops.Hadamard(),
                 ops.RotationX(0.5)):
        nbits = 5
        for bits in helper.bitprod(nbits):
            psi = state.bitstring(*bits)
            qubit = random.randint(0, nbits - 1)

            # Full matrix (O(n*n).
            op = ops.Identity(qubit) * gate * ops.Identity(nbits - qubit - 1)
            psi1 = op(psi)

            # Single Qubit (O(n))
            psi = apply_single_gate(gate, qubit, psi)

            if not psi.is_close(psi1):
                raise AssertionError('Invalid Single Gate Application.')
コード例 #15
0
  def test_swap(self):
    """Test swap gate, various indices."""

    swap = ops.Swap(0, 4)
    psi = swap(state.bitstring(1, 0, 1, 0, 0))
    self.assertTrue(psi.is_close(state.bitstring(0, 0, 1, 0, 1)))

    swap = ops.Swap(2, 0)
    psi = swap(state.bitstring(1, 0, 0))
    self.assertTrue(psi.is_close(state.bitstring(0, 0, 1)))

    op_manual = ops.Identity()**2 * swap * ops.Identity()
    psi = op_manual(state.bitstring(1, 1, 0, 1, 1, 0))
    self.assertTrue(psi.is_close(state.bitstring(1, 1, 1, 1, 0, 0)))

    psi = swap(state.bitstring(1, 1, 0, 1, 1, 0), idx=2)
    self.assertTrue(psi.is_close(state.bitstring(1, 1, 1, 1, 0, 0)))
コード例 #16
0
  def test_cnot(self):
    """Check implementation of ControlledU via Cnot."""

    psi = state.bitstring(0, 1)
    psi2 = ops.Cnot(0, 1)(psi)
    self.assertTrue(psi.is_close(psi2))

    psi2 = ops.Cnot(0, 1)(state.bitstring(1, 1))
    self.assertTrue(psi2.is_close(state.bitstring(1, 0)))

    psi2 = ops.Cnot(0, 3)(state.bitstring(1, 0, 0, 0, 1))
    self.assertTrue(psi2.is_close(state.bitstring(1, 0, 0, 1, 1)))

    psi2 = ops.Cnot(4, 0)(state.bitstring(1, 0, 0, 0, 1))
    self.assertTrue(psi2.is_close(state.bitstring(0, 0, 0, 0, 1)))
コード例 #17
0
  def test_controlled_controlled(self):
    """Toffoli gate over 4 qubits to verify that controlling works."""

    cnot = ops.Cnot(0, 3)
    toffoli = ops.ControlledU(0, 1, cnot)
    self.assertTrue(toffoli.is_close(ops.Toffoli(0, 1, 4)))

    psi = toffoli(state.bitstring(0, 1, 0, 0, 1))
    self.assertTrue(psi.is_close(state.bitstring(0, 1, 0, 0, 1)))

    psi = toffoli(state.bitstring(1, 1, 0, 0, 1))
    self.assertTrue(psi.is_close(state.bitstring(1, 1, 0, 0, 0)))

    psi = toffoli(state.bitstring(0, 0, 1, 1, 0, 0, 1), idx=2)
    self.assertTrue(psi.is_close(state.bitstring(0, 0, 1, 1, 0, 0, 0)))
コード例 #18
0
ファイル: state_test.py プロジェクト: qcc4cp/qcc
    def test_regs(self):
        a = state.Reg(3, [0, 1, 1], 0)
        b = state.Reg(3, [0, 0, 1], 3)
        psi = state.fromregs(a, b)
        psi_manual = state.bitstring(0, 1, 1, 0, 0, 1)
        self.assertEqual(a[0], 0)
        self.assertEqual(b[0], 3)
        self.assertTrue(psi.is_close(psi_manual))

        a = state.Reg(3, 1, 0)
        self.assertEqual('|001>', str(a))
        a = state.Reg(3, 6, 3)
        self.assertEqual('|110>', str(a))
        a = state.Reg(3, 7, 6)
        self.assertEqual('|111>', str(a))

        a = state.Reg(3, [1, 0, 0], 3)
        self.assertEqual('|100>', str(a))
        a = state.Reg(3, [0, 1, 1], 6)
        self.assertEqual('|011>', str(a))
        a = state.Reg(3, [1, 1, 1], 9)
        self.assertEqual('|111>', str(a))
コード例 #19
0
 def bitstring(self, *bits) -> None:
     self.psi = self.psi * state.bitstring(*bits)
     self.global_reg = self.global_reg + len(bits)
コード例 #20
0
def simple_kick():
  psi = state.bitstring(0, 0, 1)
  psi = ops.Hadamard(2)(psi)
  psi = ops.ControlledU(0, 2, ops.Sgate())(psi)
  psi = ops.ControlledU(1, 2, ops.Tgate())(psi, 1)
  psi.dump()
コード例 #21
0
 def test_controlled_rotations(self):
   psi = state.bitstring(1, 1, 1)
   psi02 = ops.ControlledU(0, 2, ops.Rk(1))(psi)
   psi20 = ops.ControlledU(2, 0, ops.Rk(1))(psi)
   self.assertTrue(psi02.is_close(psi20))
コード例 #22
0
 def bitstring(self, *bits):
     self.psi = self.psi * state.bitstring(*bits)