コード例 #1
0
ファイル: test_qft.py プロジェクト: JonathonMisiewicz/qforte
    def test_qft(self):
        trial_state = Computer(4)
        trial_circ = build_circuit('X_0 X_1')
        trial_state.apply_circuit(trial_circ)

        # verify direct transformation
        qft(trial_state, 0, 3)

        a1_dag_a2 = build_operator('1.0, Z_0')
        exp = trial_state.direct_op_exp_val(a1_dag_a2)
        assert exp == approx(0, abs=1.0e-16)

        # test unitarity
        qft(trial_state, 0, 2)
        rev_qft(trial_state, 0, 2)

        a1_dag_a2 = build_operator('1.0, Z_0')
        exp = trial_state.direct_op_exp_val(a1_dag_a2)
        assert exp == approx(0, abs=1.0e-16)

        # test reverse transformation
        qft(trial_state, 0, 3)

        a1_dag_a2 = build_operator('1.0, Z_0')
        exp = trial_state.direct_op_exp_val(a1_dag_a2)
        assert exp == approx(1.0, abs=1.0e-14)
コード例 #2
0
    def test_advanced_gates(self):
        print('\n')
        trial_state = Computer(4)
        trial_circ = build_circuit('X_0 X_1')
        trial_state.apply_circuit(trial_circ)

        # verify Toffoli gate
        T_circ = Toffoli(0, 1, 2)
        print(T_circ.str())
        trial_state.apply_circuit(T_circ)  # This should turn the state to 1110
        a1_dag_a2 = build_operator('1.0, Z_2')
        exp = trial_state.direct_op_exp_val(a1_dag_a2)
        assert exp == approx(-1, abs=9e-16)  # Measure qubit 2 should give -1

        # verify Fredkin gate
        F_circ = Fredkin(1, 2, 3)
        print(F_circ.str())
        trial_state.apply_circuit(F_circ)  # This should turn the state to 1101
        # trial_state.apply_circuit_safe(F_circ) # This should turn the state to 1101
        a1_dag_a2 = build_operator('1.0, Z_2')
        exp = trial_state.direct_op_exp_val(a1_dag_a2)
        assert exp == approx(1, abs=9e-16)  # Measure qubit 2 should give +1
コード例 #3
0
    def test_op_exp_val_1(self):
        # test direct expectation value measurement
        trial_state = Computer(4)

        trial_prep = [None] * 5
        trial_prep[0] = gate('H', 0, 0)
        trial_prep[1] = gate('H', 1, 1)
        trial_prep[2] = gate('H', 2, 2)
        trial_prep[3] = gate('H', 3, 3)
        trial_prep[4] = gate('cX', 0, 1)

        trial_circ = Circuit()

        #prepare the circuit
        for gate_ in trial_prep:
            trial_circ.add(gate_)

        # use circuit to prepare trial state
        trial_state.apply_circuit(trial_circ)

        # gates needed for [a1^ a2] operator
        X1 = gate('X', 1, 1)
        X2 = gate('X', 2, 2)
        Y1 = gate('Y', 1, 1)
        Y2 = gate('Y', 2, 2)

        # initialize circuits to make operator
        circ1 = Circuit()
        circ1.add(X2)
        circ1.add(Y1)
        circ2 = Circuit()
        circ2.add(Y2)
        circ2.add(Y1)
        circ3 = Circuit()
        circ3.add(X2)
        circ3.add(X1)
        circ4 = Circuit()
        circ4.add(Y2)
        circ4.add(X1)

        #build the quantum operator for [a1^ a2]
        a1_dag_a2 = QubitOperator()
        a1_dag_a2.add(0.0 - 0.25j, circ1)
        a1_dag_a2.add(0.25, circ2)
        a1_dag_a2.add(0.25, circ3)
        a1_dag_a2.add(0.0 + 0.25j, circ4)

        #get direct expectatoin value
        exp = trial_state.direct_op_exp_val(a1_dag_a2)
        assert exp == approx(0.25, abs=2.0e-16)
コード例 #4
0
    def test_io_simplified(self):
        # test direct expectation value measurement
        trial_state = Computer(4)
        trial_circ = build_circuit('H_0 H_1 H_2 H_3 cX_0_1')

        # use circuit to prepare trial state
        trial_state.apply_circuit(trial_circ)

        #build the quantum operator for [a1^ a2]
        a1_dag_a2 = build_operator('0.0-0.25j, X_2 Y_1; 0.25, Y_2 Y_1; \
        0.25, X_2 X_1; 0.0+0.25j, Y_2 X_1')

        #get direct expectatoin value
        exp = trial_state.direct_op_exp_val(a1_dag_a2)
        assert exp == approx(0.25, abs=2.0e-16)