def generate_circuit(N: int) -> quasar.Circuit:
    gadget = quasar.Circuit().Ry(1).CZ(0, 1).Ry(1).CX(1, 0)
    circuit = quasar.Circuit().X(0)
    for I in range(N):
        circuit.add_gates(circuit=gadget, qubits=(I, I + 1))

    parameter_values = []
    for I in range(N):
        value = (1.0 - I / 17.0)
        parameter_values.append(+value)
        parameter_values.append(-value)
    circuit.set_parameter_values(parameter_values)
    return circuit
示例#2
0
    def from_qusetta(circuit: List[str]) -> quasar.Circuit:
        """Convert a qusetta circuit to a quasar circuit.

        Parameters
        ----------
        circuit : list of strings.
            See ``help(qusetta)`` for more details on how the list of
            strings should be formatted.

        Returns
        -------
        quasar_circuit : quasar.Circuit.

        Examples
        --------
        >>> from qusetta import Quasar
        >>>
        >>> circuit = ["H(0)", "CX(0, 1)", "RX(PI/2)(0)", "SWAP(1, 2)"]
        >>> quasar_circuit = Quasar.from_qusetta(circuit)

        """
        quasar_circuit = quasar.Circuit()
        for gate in circuit:
            g, params, qubits = qs.gate_info(gate)
            # qusetta's angles are twice what quasars are
            params = tuple(x / 2 for x in params)
            getattr(quasar_circuit, MAPPING.get(g, g))(*(qubits + params))
        return quasar_circuit
示例#3
0
    def test_shifted_sparse_circuit(self):

        circuit = quasar.Circuit()
        circuit.add_gate(quasar.Gate.H, -2, times=-2, copy=False)
        circuit.add_gate(quasar.Gate.CX, (0, 1), times=(2, ), copy=False)
        circuit.add_gate(quasar.Gate.CX,
                         qubits=(3, 4),
                         times=(4, ),
                         copy=False)

        self.assertEqual(circuit.ngate, 3)
        self.assertEqual(circuit.ngate1, 1)
        self.assertEqual(circuit.ngate2, 2)
        self.assertEqual(circuit.ngate3, 0)
        self.assertEqual(circuit.ngate4, 0)
        self.assertEqual(circuit.ngate_nqubit(0), 0)
        self.assertEqual(circuit.ngate_nqubit(1), 1)
        self.assertEqual(circuit.ngate_nqubit(2), 2)
        self.assertEqual(circuit.ngate_nqubit(3), 0)
        self.assertEqual(circuit.ngate_nqubit(4), 0)
        self.assertEqual(circuit.max_gate_nqubit, 2)
        self.assertEqual(circuit.max_gate_ntime, 1)
        self.assertEqual(circuit.min_time, -2)
        self.assertEqual(circuit.max_time, 4)
        self.assertEqual(circuit.ntime, 7)
        self.assertEqual(circuit.ntime_sparse, 3)
        self.assertEqual(circuit.min_qubit, -2)
        self.assertEqual(circuit.max_qubit, 4)
        self.assertEqual(circuit.nqubit, 7)
        self.assertEqual(circuit.nqubit_sparse, 5)
        self.assertFalse(circuit.is_controlled)
        self.assertFalse(circuit.is_composite)

        self.assertIsInstance(circuit, quasar.Circuit)
        self.assertIsInstance(circuit.gates, sortedcontainers.SortedDict)
        self.assertIsInstance(circuit.times, sortedcontainers.SortedSet)
        self.assertIsInstance(circuit.qubits, sortedcontainers.SortedSet)
        self.assertIsInstance(circuit.times_and_qubits,
                              sortedcontainers.SortedSet)

        self.assertEqual(
            circuit.gates,
            sortedcontainers.SortedDict([
                (((-2, ), (-2, )), quasar.Gate.H),
                (((2, ), (0, 1)), quasar.Gate.CX),
                (((4, ), (3, 4)), quasar.Gate.CX),
            ]))
        self.assertEqual(circuit.times, sortedcontainers.SortedSet([-2, 2, 4]))
        self.assertEqual(circuit.qubits,
                         sortedcontainers.SortedSet([-2, 0, 1, 3, 4]))
        self.assertEqual(
            circuit.times_and_qubits,
            sortedcontainers.SortedSet([(-2, -2), (2, 0), (2, 1), (4, 3),
                                        (4, 4)]))

        self.assertIsInstance(str(circuit), str)
示例#4
0
    def test_ghz_circuit(self):

        circuit = quasar.Circuit()
        circuit.add_gate(quasar.Gate.H, 0, copy=False)
        circuit.add_gate(quasar.Gate.CX, (0, 1), copy=False)
        circuit.add_gate(quasar.Gate.CX, qubits=(1, 2), copy=False)

        self.assertEqual(circuit.ngate, 3)
        self.assertEqual(circuit.ngate1, 1)
        self.assertEqual(circuit.ngate2, 2)
        self.assertEqual(circuit.ngate3, 0)
        self.assertEqual(circuit.ngate4, 0)
        self.assertEqual(circuit.ngate_nqubit(0), 0)
        self.assertEqual(circuit.ngate_nqubit(1), 1)
        self.assertEqual(circuit.ngate_nqubit(2), 2)
        self.assertEqual(circuit.ngate_nqubit(3), 0)
        self.assertEqual(circuit.ngate_nqubit(4), 0)
        self.assertEqual(circuit.max_gate_nqubit, 2)
        self.assertEqual(circuit.max_gate_ntime, 1)
        self.assertEqual(circuit.min_time, 0)
        self.assertEqual(circuit.max_time, 2)
        self.assertEqual(circuit.ntime, 3)
        self.assertEqual(circuit.ntime_sparse, 3)
        self.assertEqual(circuit.min_qubit, 0)
        self.assertEqual(circuit.max_qubit, 2)
        self.assertEqual(circuit.nqubit, 3)
        self.assertEqual(circuit.nqubit_sparse, 3)
        self.assertFalse(circuit.is_controlled)
        self.assertFalse(circuit.is_composite)

        self.assertIsInstance(circuit, quasar.Circuit)
        self.assertIsInstance(circuit.gates, sortedcontainers.SortedDict)
        self.assertIsInstance(circuit.times, sortedcontainers.SortedSet)
        self.assertIsInstance(circuit.qubits, sortedcontainers.SortedSet)
        self.assertIsInstance(circuit.times_and_qubits,
                              sortedcontainers.SortedSet)

        self.assertEqual(
            circuit.gates,
            sortedcontainers.SortedDict([
                (((0, ), (0, )), quasar.Gate.H),
                (((1, ), (0, 1)), quasar.Gate.CX),
                (((2, ), (1, 2)), quasar.Gate.CX),
            ]))
        self.assertEqual(circuit.times, sortedcontainers.SortedSet([0, 1, 2]))
        self.assertEqual(circuit.qubits, sortedcontainers.SortedSet([0, 1, 2]))
        self.assertEqual(
            circuit.times_and_qubits,
            sortedcontainers.SortedSet([(0, 0), (1, 0), (1, 1), (2, 1),
                                        (2, 2)]))

        self.assertIsInstance(str(circuit), str)
示例#5
0
文件: test.py 项目: jlapeyre/quasar
def test_ghz_5():

    circuit = quasar.Circuit(N=5)
    circuit.add_gate(T=0, key=0, gate=quasar.Gate.H)
    circuit.add_gate(T=1, key=(0, 1), gate=quasar.Gate.CNOT)
    circuit.add_gate(T=2, key=(1, 2), gate=quasar.Gate.CNOT)
    circuit.add_gate(T=3, key=(2, 3), gate=quasar.Gate.CNOT)
    circuit.add_gate(T=4, key=3, gate=quasar.Gate.H)
    circuit.add_gate(T=4, key=4, gate=quasar.Gate.H)
    circuit.add_gate(T=5, key=(4, 3), gate=quasar.Gate.CNOT)
    circuit.add_gate(T=6, key=3, gate=quasar.Gate.H)
    circuit.add_gate(T=6, key=4, gate=quasar.Gate.H)

    print(circuit)

    print(circuit.compressed())

    print(circuit.Ts)
def test_run_measurement(backend):
    q = quasar.Circuit()
    q.H(0).CX(0, 1)
    b = QuasarBackend(backend)
    result = b.run_measurement(circuit=q)
    assert isinstance(result, quasar.ProbabilityHistogram)
    assert isinstance(result.histogram, dict)
    assert 0 in result.histogram
    # yeah, pretty fuzzy but I'll take it
    assert abs(result.histogram[0] - 0.5) < 0.05

    # now try with the backend
    backend = QuasarBackend(backend)
    result = backend.run_measurement(circuit=q)
    assert isinstance(result, quasar.ProbabilityHistogram)
    assert isinstance(result.histogram, dict)
    assert 0 in result.histogram
    # yeah, pretty fuzzy but I'll take it
    assert abs(result.histogram[0] - 0.5) < 0.05
示例#7
0
    def test_empty_circuit(self):

        circuit = quasar.Circuit()

        self.assertEqual(circuit.ngate, 0)
        self.assertEqual(circuit.ngate1, 0)
        self.assertEqual(circuit.ngate2, 0)
        self.assertEqual(circuit.ngate3, 0)
        self.assertEqual(circuit.ngate4, 0)
        self.assertEqual(circuit.ngate_nqubit(0), 0)
        self.assertEqual(circuit.ngate_nqubit(1), 0)
        self.assertEqual(circuit.ngate_nqubit(2), 0)
        self.assertEqual(circuit.ngate_nqubit(3), 0)
        self.assertEqual(circuit.ngate_nqubit(4), 0)
        self.assertEqual(circuit.max_gate_nqubit, 0)
        self.assertEqual(circuit.max_gate_ntime, 0)
        self.assertEqual(circuit.min_time, 0)
        self.assertEqual(circuit.max_time, -1)
        self.assertEqual(circuit.ntime, 0)
        self.assertEqual(circuit.ntime_sparse, 0)
        self.assertEqual(circuit.min_qubit, 0)
        self.assertEqual(circuit.max_qubit, -1)
        self.assertEqual(circuit.nqubit, 0)
        self.assertEqual(circuit.nqubit_sparse, 0)
        self.assertFalse(circuit.is_controlled)
        self.assertFalse(circuit.is_composite)

        self.assertIsInstance(circuit, quasar.Circuit)
        self.assertIsInstance(circuit.gates, sortedcontainers.SortedDict)
        self.assertIsInstance(circuit.times, sortedcontainers.SortedSet)
        self.assertIsInstance(circuit.qubits, sortedcontainers.SortedSet)
        self.assertIsInstance(circuit.times_and_qubits,
                              sortedcontainers.SortedSet)

        self.assertEqual(circuit.gates, sortedcontainers.SortedDict([]))
        self.assertEqual(circuit.times, sortedcontainers.SortedSet([]))
        self.assertEqual(circuit.qubits, sortedcontainers.SortedSet([]))
        self.assertEqual(circuit.times_and_qubits,
                         sortedcontainers.SortedSet([]))

        self.assertIsInstance(str(circuit), str)
示例#8
0
def test_circuit_2():
    # test that we ignore measurement gates

    qusetta_circuit = ["H(0)"]

    q = cirq.LineQubit(0)
    cirq_circuit = cirq.Circuit(cirq.H(q), cirq.measure(q))
    cirq_circuit = qs.Cirq.from_qusetta(qs.Cirq.to_qusetta(cirq_circuit))

    qiskit_circuit = qiskit.QuantumCircuit(1, 1)
    qiskit_circuit.h(0)
    qiskit_circuit.measure(0, 0)
    qiskit_circuit = qs.Qiskit.from_qusetta(
        qs.Qiskit.to_qusetta(qiskit_circuit))

    quasar_circuit = quasar.Circuit()
    quasar_circuit.H(0)
    quasar_circuit = qs.Quasar.from_qusetta(
        qs.Quasar.to_qusetta(quasar_circuit))

    # tests
    all_tests(qusetta_circuit, cirq_circuit, qiskit_circuit, quasar_circuit)
示例#9
0
文件: test.py 项目: jlapeyre/quasar
def test_linear_4():

    circuit = quasar.Circuit(N=4)
    circuit.add_gate(T=1,
                     key=(1, 2),
                     gate=quasar.Gate.SO4(A=0.0,
                                          B=0.0,
                                          C=0.0,
                                          D=0.0,
                                          E=0.0,
                                          F=0.0))
    circuit.add_gate(T=1,
                     key=(3, 0),
                     gate=quasar.Gate.SO4(A=0.0,
                                          B=0.0,
                                          C=0.0,
                                          D=0.0,
                                          E=0.0,
                                          F=0.0))
    circuit.add_gate(T=0,
                     key=(0, 1),
                     gate=quasar.Gate.SO4(A=0.0,
                                          B=0.0,
                                          C=0.0,
                                          D=0.0,
                                          E=0.0,
                                          F=0.0))
    circuit.add_gate(T=0,
                     key=(2, 3),
                     gate=quasar.Gate.SO4(A=0.0,
                                          B=0.0,
                                          C=0.0,
                                          D=0.0,
                                          E=0.0,
                                          F=0.0))
    print(circuit)
    print(circuit.param_str)
示例#10
0
    def test_circuit_equivalence(self):

        # Reflexivity
        circuit1 = quasar.Circuit().H(0).CX(0, 1)
        self.assertTrue(quasar.Circuit.test_equivalence(circuit1, circuit1))

        # Reflexivity (different objects)
        circuit1 = quasar.Circuit().H(0).CX(0, 1)
        circuit2 = quasar.Circuit().H(0).CX(0, 1)
        self.assertTrue(quasar.Circuit.test_equivalence(circuit1, circuit2))

        # Reflexivity (different build order)
        circuit1 = quasar.Circuit().CX(0, 1, times=1).H(0, times=0)
        circuit2 = quasar.Circuit().H(0).CX(0, 1)
        self.assertTrue(quasar.Circuit.test_equivalence(circuit1, circuit2))

        # Same layout, different gates
        circuit1 = quasar.Circuit().Z(0).CX(0, 1)
        circuit2 = quasar.Circuit().H(0).CX(0, 1)
        self.assertFalse(quasar.Circuit.test_equivalence(circuit1, circuit2))

        # Same layout, different times
        circuit1 = quasar.Circuit().H(0).CX(0, 1)
        circuit2 = quasar.Circuit().H(0, time_start=-1).CX(1, 2)
        self.assertFalse(quasar.Circuit.test_equivalence(circuit1, circuit2))

        # Same layout, different qubits
        circuit1 = quasar.Circuit().H(0).CX(0, 1)
        circuit2 = quasar.Circuit().H(1).CX(1, 2)
        self.assertFalse(quasar.Circuit.test_equivalence(circuit1, circuit2))
示例#11
0
def test_circuit_0():
    qusetta_circuit = [
        "H(0)", "H(1)", "CX(0, 1)", "CX(1, 0)", "CZ(2, 0)", "I(1)",
        "SWAP(0, 3)", "RY(PI)(1)", "X(2)", "S(0)", "Z(2)", "Y(3)",
        "RX(0.4*PI)(0)", "T(2)", "RZ(-0.3*PI)(2)", "CCX(0, 1, 2)"
    ]

    cirq_circuit = cirq.Circuit()
    q = [cirq.LineQubit(i) for i in range(4)]
    cirq_circuit.append(cirq.H(q[0]))
    cirq_circuit.append(cirq.H(q[1]))
    cirq_circuit.append(cirq.CX(q[0], q[1]))
    cirq_circuit.append(cirq.CX(q[1], q[0]))
    cirq_circuit.append(cirq.CZ(q[2], q[0]))
    cirq_circuit.append(cirq.I(q[1]))
    cirq_circuit.append(cirq.SWAP(q[0], q[3]))
    cirq_circuit.append(cirq.ry(pi)(q[1]))
    cirq_circuit.append(cirq.X(q[2]))
    cirq_circuit.append(cirq.S(q[0]))
    cirq_circuit.append(cirq.Z(q[2]))
    cirq_circuit.append(cirq.Y(q[3]))
    cirq_circuit.append(cirq.rx(.4 * pi)(q[0]))
    cirq_circuit.append(cirq.T(q[2]))
    cirq_circuit.append(cirq.rz(-.3 * pi)(q[2]))
    cirq_circuit.append(cirq.CCX(q[0], q[1], q[2]))

    # ibm is weird so we flip all of the qubits here
    qiskit_circuit = qiskit.QuantumCircuit(4)
    qiskit_circuit.h(3 - 0)
    qiskit_circuit.h(3 - 1)
    qiskit_circuit.cx(3 - 0, 3 - 1)
    qiskit_circuit.cx(3 - 1, 3 - 0)
    qiskit_circuit.cz(3 - 2, 3 - 0)
    qiskit_circuit.i(3 - 1)
    qiskit_circuit.swap(3 - 0, 3 - 3)
    qiskit_circuit.ry(pi, 3 - 1)
    qiskit_circuit.x(3 - 2)
    qiskit_circuit.s(3 - 0)
    qiskit_circuit.z(3 - 2)
    qiskit_circuit.y(3 - 3)
    qiskit_circuit.rx(.4 * pi, 3 - 0)
    qiskit_circuit.t(3 - 2)
    qiskit_circuit.rz(-.3 * pi, 3 - 2)
    qiskit_circuit.ccx(3 - 0, 3 - 1, 3 - 2)

    quasar_circuit = quasar.Circuit()
    quasar_circuit.H(0)
    quasar_circuit.H(1)
    quasar_circuit.CX(0, 1)
    quasar_circuit.CX(1, 0)
    quasar_circuit.CZ(2, 0)
    quasar_circuit.I(1)
    quasar_circuit.SWAP(0, 3)
    quasar_circuit.Ry(1, pi)
    quasar_circuit.X(2)
    quasar_circuit.S(0)
    quasar_circuit.Z(2)
    quasar_circuit.Y(3)
    quasar_circuit.Rx(0, .2 * pi)
    quasar_circuit.T(2)
    quasar_circuit.Rz(2, -.15 * pi)
    quasar_circuit.CCX(0, 1, 2)

    # tests
    all_tests(qusetta_circuit, cirq_circuit, qiskit_circuit, quasar_circuit)