def create_circuit(formula, circuit, qubits, indexator, start, end, negated):
    size = end - start  #calcula o tamanho do que está entre parênteses
    i = start
    current_result = -1
    while i < end and len(indexator) > 1 and size > 1:
        #print(i)
        #print(indexator)
        if formula[indexator[i]] == '(' or formula[
                indexator[i]] == ')' or formula[indexator[i]] == '[':
            print("Erro: parentese encontrado fora do lugar")
            exit()
        if formula[indexator[i]] == '|':
            circuit.append(cirq.X(
                qubits[indexator[i]]))  #flipa o bit auxiliar, setado em 0
            circuit.append(
                cirq.FREDKIN(
                    qubits[indexator[i - 1]], qubits[indexator[i + 1]],
                    qubits[indexator[i]]))  #faz o gate de Fredkin em OR
            #resultado está em qubits[i + 1]
            current_result = indexator[i + 1]
            del indexator[i - 1]
            del indexator[i - 1]
            size = size - 2
        elif formula[indexator[i]] == '&':
            formula[indexator[i]] = '_'
            circuit.append(
                cirq.FREDKIN(
                    qubits[indexator[i - 1]], qubits[indexator[i + 1]],
                    qubits[indexator[i]]))  #faz o gate de Fredkin em AND
            #resultado está em qubits[i]
            current_result = indexator[i]
            del indexator[i - 1]
            del indexator[i]
            size = size - 2
        elif formula[indexator[i]] == '>':  #flipa o resultado atual a faz o OR
            circuit.append(cirq.X(qubits[indexator[i - 1]]))
            circuit.append(cirq.X(qubits[indexator[i]]))
            circuit.append(
                cirq.FREDKIN(
                    qubits[indexator[i - 1]], qubits[indexator[i + 1]],
                    qubits[indexator[i]]))  #faz o gate de Fredkin em OR
            current_result = indexator[i + 1]
            del indexator[i - 1]
            del indexator[i - 1]
            size = size - 2
        else:
            i = i + 1
    if negated == 1:
        circuit.append(cirq.X(qubits[current_result]))
def test_diagram():
    a, b, c, d = cirq.LineQubit.range(4)
    circuit = cirq.Circuit.from_ops(cirq.TOFFOLI(a, b, c),
                                    cirq.TOFFOLI(a, b, c)**0.5,
                                    cirq.CCX(a, c, b), cirq.CCZ(a, d, b),
                                    cirq.CCZ(a, d, b)**0.5,
                                    cirq.CSWAP(a, c, d), cirq.FREDKIN(a, b, c))
    cirq.testing.assert_has_diagram(
        circuit, """
0: ───@───@───────@───@───@───────@───@───
      │   │       │   │   │       │   │
1: ───@───@───────X───@───@───────┼───×───
      │   │       │   │   │       │   │
2: ───X───X^0.5───@───┼───┼───────×───×───
                      │   │       │
3: ───────────────────@───@^0.5───×───────
""")
    cirq.testing.assert_has_diagram(circuit,
                                    """
0: ---@---@-------@---@---@-------@------@------
      |   |       |   |   |       |      |
1: ---@---@-------X---@---@-------|------swap---
      |   |       |   |   |       |      |
2: ---X---X^0.5---@---|---|-------swap---swap---
                      |   |       |
3: -------------------@---@^0.5---swap----------
""",
                                    use_unicode_characters=False)
Exemple #3
0
def test_diagram():
    a, b, c, d = cirq.LineQubit.range(4)
    circuit = cirq.Circuit(
        cirq.TOFFOLI(a, b, c),
        cirq.TOFFOLI(a, b, c)**0.5,
        cirq.CCX(a, c, b),
        cirq.CCZ(a, d, b),
        cirq.CCZ(a, d, b)**0.5,
        cirq.CSWAP(a, c, d),
        cirq.FREDKIN(a, b, c),
    )
    cirq.testing.assert_has_diagram(
        circuit,
        """
0: ───@───@───────@───@───@───────@───@───
      │   │       │   │   │       │   │
1: ───@───@───────X───@───@───────┼───×───
      │   │       │   │   │       │   │
2: ───X───X^0.5───@───┼───┼───────×───×───
                      │   │       │
3: ───────────────────@───@^0.5───×───────
""",
    )
    cirq.testing.assert_has_diagram(
        circuit,
        """
0: ---@---@-------@---@---@-------@------@------
      |   |       |   |   |       |      |
1: ---@---@-------X---@---@-------|------swap---
      |   |       |   |   |       |      |
2: ---X---X^0.5---@---|---|-------swap---swap---
                      |   |       |
3: -------------------@---@^0.5---swap----------
""",
        use_unicode_characters=False,
    )

    diagonal_circuit = cirq.Circuit(
        cirq.ThreeQubitDiagonalGate([2, 3, 5, 7, 11, 13, 17, 19])(a, b, c))
    cirq.testing.assert_has_diagram(
        diagonal_circuit,
        """
0: ───diag(2, 3, 5, 7, 11, 13, 17, 19)───
      │
1: ───#2─────────────────────────────────
      │
2: ───#3─────────────────────────────────
""",
    )
    cirq.testing.assert_has_diagram(
        diagonal_circuit,
        """
0: ---diag(2, 3, 5, 7, 11, 13, 17, 19)---
      |
1: ---#2---------------------------------
      |
2: ---#3---------------------------------
""",
        use_unicode_characters=False,
    )
Exemple #4
0
def test_eq():
    a, b, c, d = cirq.LineQubit.range(4)
    eq = cirq.testing.EqualsTester()
    eq.add_equality_group(cirq.CCZ(a, b, c), cirq.CCZ(a, c, b),
                          cirq.CCZ(b, c, a))
    eq.add_equality_group(cirq.CCZ(a, b, d))
    eq.add_equality_group(cirq.TOFFOLI(a, b, c), cirq.CCX(a, b, c))
    eq.add_equality_group(cirq.TOFFOLI(a, c, b), cirq.TOFFOLI(c, a, b))
    eq.add_equality_group(cirq.TOFFOLI(a, b, d))
    eq.add_equality_group(cirq.CSWAP(a, b, c), cirq.FREDKIN(a, b, c))
    eq.add_equality_group(cirq.CSWAP(b, a, c), cirq.CSWAP(b, c, a))
def create_circuit(formula, circuit, qubits, indexator, start, end):
		size = end - start #calcula o tamanho do que está entre parênteses

		print("CREATE CIRCUIT CALLED ---------------------")
		#print(formula)
		print(start)
		print(end)
		i = start
		while i < end and len(indexator) > 1 and size > 1:
			print(i)
			print(indexator)
			print(formula)
			if formula[indexator[i]] == '(' or formula[indexator[i]] == ')':
				print("ERRO")
				exit()
			if formula[indexator[i]] == '|':
				circuit.append(cirq.X(qubits[indexator[i]])) #flipa o bit auxiliar, setado em 0
				circuit.append(cirq.FREDKIN(qubits[indexator[i - 1]], qubits[indexator[i + 1]], qubits[indexator[i]])) #faz o gate de Fredkin em OR
				#resultado está em qubits[i + 1]
				del indexator[i - 1]
				del indexator[i - 1]
				size = size - 2
			elif formula[indexator[i]] == '&':
				formula[indexator[i]] = '_'
				circuit.append(cirq.FREDKIN(qubits[indexator[i - 1]], qubits[indexator[i + 1]], qubits[indexator[i]])) #faz o gate de Fredkin em AND
				#resultado está em qubits[i]
				del indexator[i - 1]
				del indexator[i]
				size = size - 2
			elif formula[indexator[i]] == '>': #flipa o resultado atual a faz o OR
				print("IMPLICATION")
				circuit.append(cirq.X(qubits[indexator[i - 1]]))
				circuit.append(cirq.X(qubits[indexator[i]]))
				circuit.append(cirq.FREDKIN(qubits[indexator[i - 1]], qubits[indexator[i + 1]], qubits[indexator[i]])) #faz o gate de Fredkin em OR
				del indexator[i - 1]
				del indexator[i - 1]
				size = size - 2
			else:
				i = i + 1
		print("CREATE CIRCUIT FINISHED ---------------------")
def test_fredkin():
    a, b, c = cirq.LineQubit.range(3)
    circuit = cirq.Circuit(cirq.FREDKIN(a, b, c))
    assert_links_to(circuit, """
        http://algassert.com/quirk#circuit={"cols":[["•","Swap","Swap"]]}
    """, escape_url=False)

    # Doesn't merge.
    x, y, z = cirq.LineQubit.range(3, 6)
    circuit = cirq.Circuit(cirq.CSWAP(a, b, c), cirq.CSWAP(x, y, z))
    assert_links_to(circuit, """
        http://algassert.com/quirk#circuit={"cols":[
            ["•","Swap","Swap"],
            [1,1,1,"•","Swap","Swap"]
        ]}
    """, escape_url=False)
Exemple #7
0
def test_diagram():
    a, b, c, d = cirq.LineQubit.range(4)
    circuit = cirq.Circuit.from_ops(cirq.TOFFOLI(a, b, c), cirq.CCX(a, c, b),
                                    cirq.CCZ(a, d, b), cirq.CSWAP(a, c, d),
                                    cirq.FREDKIN(a, b, c))
    assert circuit.to_text_diagram() == """
0: ───@───@───@───@───@───
      │   │   │   │   │
1: ───@───X───@───┼───×───
      │   │   │   │   │
2: ───X───@───┼───×───×───
              │   │
3: ───────────@───×───────
""".strip()
    assert circuit.to_text_diagram(use_unicode_characters=False) == """
0: ---@---@---@---@------@------
      |   |   |   |      |
1: ---@---X---@---|------swap---
      |   |   |   |      |
2: ---X---@---|---swap---swap---
              |   |
3: -----------@---swap----------
""".strip()
Exemple #8
0
        b -= gate

        prefix = gates[:i + 1]
        expected_a = cirq.LinearCombinationOfGates(collections.Counter(prefix))
        expected_b = -expected_a

        assert_linear_combinations_are_equal(a, expected_a)
        assert_linear_combinations_are_equal(b, expected_b)


@pytest.mark.parametrize('op', (
    cirq.X(q0),
    cirq.Y(q1),
    cirq.XX(q0, q1),
    cirq.CZ(q0, q1),
    cirq.FREDKIN(q0, q1, q2),
    cirq.ControlledOperation((q0, q1), cirq.H(q2)),
    cirq.ParallelGateOperation(cirq.X, (q0, q1, q2)),
    cirq.PauliString({
        q0: cirq.X,
        q1: cirq.Y,
        q2: cirq.Z
    }),
))
def test_empty_linear_combination_of_operations_accepts_all_operations(op):
    combination = cirq.LinearCombinationOfOperations({})
    combination[op] = -0.5j
    assert len(combination) == 1


@pytest.mark.parametrize('terms', (
Exemple #9
0
def test_cirq_qsim_all_supported_gates():
    q0 = cirq.GridQubit(1, 1)
    q1 = cirq.GridQubit(1, 0)
    q2 = cirq.GridQubit(0, 1)
    q3 = cirq.GridQubit(0, 0)

    circuit = cirq.Circuit(
        cirq.Moment(
            cirq.H(q0),
            cirq.H(q1),
            cirq.H(q2),
            cirq.H(q3),
        ),
        cirq.Moment(
            cirq.T(q0),
            cirq.T(q1),
            cirq.T(q2),
            cirq.T(q3),
        ),
        cirq.Moment(
            cirq.CZPowGate(exponent=0.7, global_shift=0.2)(q0, q1),
            cirq.CXPowGate(exponent=1.2, global_shift=0.4)(q2, q3),
        ),
        cirq.Moment(
            cirq.XPowGate(exponent=0.3, global_shift=1.1)(q0),
            cirq.YPowGate(exponent=0.4, global_shift=1)(q1),
            cirq.ZPowGate(exponent=0.5, global_shift=0.9)(q2),
            cirq.HPowGate(exponent=0.6, global_shift=0.8)(q3),
        ),
        cirq.Moment(
            cirq.CX(q0, q2),
            cirq.CZ(q1, q3),
        ),
        cirq.Moment(
            cirq.X(q0),
            cirq.Y(q1),
            cirq.Z(q2),
            cirq.S(q3),
        ),
        cirq.Moment(
            cirq.XXPowGate(exponent=0.4, global_shift=0.7)(q0, q1),
            cirq.YYPowGate(exponent=0.8, global_shift=0.5)(q2, q3),
        ),
        cirq.Moment(cirq.I(q0), cirq.I(q1), cirq.IdentityGate(2)(q2, q3)),
        cirq.Moment(
            cirq.rx(0.7)(q0),
            cirq.ry(0.2)(q1),
            cirq.rz(0.4)(q2),
            cirq.PhasedXPowGate(phase_exponent=0.8, exponent=0.6, global_shift=0.3)(q3),
        ),
        cirq.Moment(
            cirq.ZZPowGate(exponent=0.3, global_shift=1.3)(q0, q2),
            cirq.ISwapPowGate(exponent=0.6, global_shift=1.2)(q1, q3),
        ),
        cirq.Moment(
            cirq.XPowGate(exponent=0.1, global_shift=0.9)(q0),
            cirq.YPowGate(exponent=0.2, global_shift=1)(q1),
            cirq.ZPowGate(exponent=0.3, global_shift=1.1)(q2),
            cirq.HPowGate(exponent=0.4, global_shift=1.2)(q3),
        ),
        cirq.Moment(
            cirq.SwapPowGate(exponent=0.2, global_shift=0.9)(q0, q1),
            cirq.PhasedISwapPowGate(phase_exponent=0.8, exponent=0.6)(q2, q3),
        ),
        cirq.Moment(
            cirq.PhasedXZGate(x_exponent=0.2, z_exponent=0.3, axis_phase_exponent=1.4)(
                q0
            ),
            cirq.T(q1),
            cirq.H(q2),
            cirq.S(q3),
        ),
        cirq.Moment(
            cirq.SWAP(q0, q2),
            cirq.XX(q1, q3),
        ),
        cirq.Moment(
            cirq.rx(0.8)(q0),
            cirq.ry(0.9)(q1),
            cirq.rz(1.2)(q2),
            cirq.T(q3),
        ),
        cirq.Moment(
            cirq.YY(q0, q1),
            cirq.ISWAP(q2, q3),
        ),
        cirq.Moment(
            cirq.T(q0),
            cirq.Z(q1),
            cirq.Y(q2),
            cirq.X(q3),
        ),
        cirq.Moment(
            cirq.FSimGate(0.3, 1.7)(q0, q2),
            cirq.ZZ(q1, q3),
        ),
        cirq.Moment(
            cirq.ry(1.3)(q0),
            cirq.rz(0.4)(q1),
            cirq.rx(0.7)(q2),
            cirq.S(q3),
        ),
        cirq.Moment(
            cirq.IdentityGate(4).on(q0, q1, q2, q3),
        ),
        cirq.Moment(
            cirq.CCZPowGate(exponent=0.7, global_shift=0.3)(q2, q0, q1),
        ),
        cirq.Moment(
            cirq.CCXPowGate(exponent=0.4, global_shift=0.6)(q3, q1, q0).controlled_by(
                q2, control_values=[0]
            ),
        ),
        cirq.Moment(
            cirq.rx(0.3)(q0),
            cirq.ry(0.5)(q1),
            cirq.rz(0.7)(q2),
            cirq.rx(0.9)(q3),
        ),
        cirq.Moment(
            cirq.TwoQubitDiagonalGate([0.1, 0.2, 0.3, 0.4])(q0, q1),
        ),
        cirq.Moment(
            cirq.ThreeQubitDiagonalGate([0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.2, 1.3])(
                q1, q2, q3
            ),
        ),
        cirq.Moment(
            cirq.CSwapGate()(q0, q3, q1),
        ),
        cirq.Moment(
            cirq.rz(0.6)(q0),
            cirq.rx(0.7)(q1),
            cirq.ry(0.8)(q2),
            cirq.rz(0.9)(q3),
        ),
        cirq.Moment(
            cirq.TOFFOLI(q3, q2, q0),
        ),
        cirq.Moment(
            cirq.FREDKIN(q1, q3, q2),
        ),
        cirq.Moment(
            cirq.MatrixGate(
                np.array(
                    [
                        [0, -0.5 - 0.5j, -0.5 - 0.5j, 0],
                        [0.5 - 0.5j, 0, 0, -0.5 + 0.5j],
                        [0.5 - 0.5j, 0, 0, 0.5 - 0.5j],
                        [0, -0.5 - 0.5j, 0.5 + 0.5j, 0],
                    ]
                )
            )(q0, q1),
            cirq.MatrixGate(
                np.array(
                    [
                        [0.5 - 0.5j, 0, 0, -0.5 + 0.5j],
                        [0, 0.5 - 0.5j, -0.5 + 0.5j, 0],
                        [0, -0.5 + 0.5j, -0.5 + 0.5j, 0],
                        [0.5 - 0.5j, 0, 0, 0.5 - 0.5j],
                    ]
                )
            )(q2, q3),
        ),
        cirq.Moment(
            cirq.MatrixGate(np.array([[1, 0], [0, 1j]]))(q0),
            cirq.MatrixGate(np.array([[0, -1j], [1j, 0]]))(q1),
            cirq.MatrixGate(np.array([[0, 1], [1, 0]]))(q2),
            cirq.MatrixGate(np.array([[1, 0], [0, -1]]))(q3),
        ),
        cirq.Moment(
            cirq.riswap(0.7)(q0, q1),
            cirq.givens(1.2)(q2, q3),
        ),
        cirq.Moment(
            cirq.H(q0),
            cirq.H(q1),
            cirq.H(q2),
            cirq.H(q3),
        ),
    )

    simulator = cirq.Simulator()
    cirq_result = simulator.simulate(circuit)

    qsim_simulator = qsimcirq.QSimSimulator()
    qsim_result = qsim_simulator.simulate(circuit)

    assert cirq.linalg.allclose_up_to_global_phase(
        qsim_result.state_vector(), cirq_result.state_vector()
    )