def test_ilo() -> None: b = AerBackend() bs = AerStateBackend() bu = AerUnitaryBackend() c = Circuit(2) c.X(1) assert (bs.get_state(c) == np.asarray([0, 1, 0, 0])).all() assert (bs.get_state(c, basis=BasisOrder.dlo) == np.asarray([0, 0, 1, 0])).all() assert (bu.get_unitary(c) == np.asarray([[0, 1, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]])).all() assert (bu.get_unitary(c, basis=BasisOrder.dlo) == np.asarray([[0, 0, 1, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 1, 0, 0]])).all() c.measure_all() assert (b.get_shots(c, 2) == np.asarray([[0, 1], [0, 1]])).all() assert (b.get_shots(c, 2, basis=BasisOrder.dlo) == np.asarray([[1, 0], [1, 0]])).all() assert b.get_counts(c, 2) == {(0, 1): 2} assert b.get_counts(c, 2, basis=BasisOrder.dlo) == {(1, 0): 2}
def test_statevector() -> None: c = circuit_gen() b = AerStateBackend() state = b.get_state(c) assert np.allclose( state, [math.sqrt(0.5), 0, 0, math.sqrt(0.5)], atol=1e-10) c.add_phase(0.5) state1 = b.get_state(c) assert np.allclose(state1, state * 1j, atol=1e-10)
def test_state() -> None: qc = circuit_gen() b = AerStateBackend() for comp in (None, b.default_compilation_pass()): tb = TketBackend(b, comp) assert QuantumInstance(tb).is_statevector job = execute(qc, tb) state = job.result().get_statevector() qb = Aer.get_backend("statevector_simulator") job2 = execute(qc, qb) state2 = job2.result().get_statevector() assert np.allclose(state, state2)
def test_aerstate_result_handle() -> None: c = circuit_gen() b1 = AerStateBackend() h1 = b1.process_circuits([c])[0] state = b1.get_result(h1).get_state() status = b1.circuit_status(h1) assert status == CircuitStatus(StatusEnum.COMPLETED, "job has successfully run") assert np.allclose(state, [np.sqrt(0.5), 0, 0, math.sqrt(0.5)], atol=1e-10) b2 = AerUnitaryBackend() unitary = b2.get_unitary(c) assert np.allclose( unitary, np.sqrt(0.5) * np.array([[1, 0, 1, 0], [0, 1, 0, 1], [0, 1, 0, -1], [1, 0, -1, 0]]), )
def test_operator() -> None: for b in [AerBackend(), AerStateBackend()]: c = circuit_gen() zz = QubitPauliOperator( {QubitPauliString([Qubit(0), Qubit(1)], [Pauli.Z, Pauli.Z]): 1.0}) assert cmath.isclose(get_operator_expectation_value(c, zz, b), 1.0) c.X(0) assert cmath.isclose(get_operator_expectation_value(c, zz, b), -1.0)
def test_pauli() -> None: for b in [AerBackend(), AerStateBackend()]: c = Circuit(2) c.Rz(0.5, 0) b.compile_circuit(c) zi = QubitPauliString(Qubit(0), Pauli.Z) assert cmath.isclose(get_pauli_expectation_value(c, zi, b), 1) c.X(0) assert cmath.isclose(get_pauli_expectation_value(c, zi, b), -1)
def test_pauli_statevector() -> None: c = Circuit(2) c.Rz(0.5, 0) Transform.OptimisePostRouting().apply(c) b = AerStateBackend() zi = QubitPauliString(Qubit(0), Pauli.Z) assert get_pauli_expectation_value(c, zi, b) == 1 c.X(0) assert get_pauli_expectation_value(c, zi, b) == -1
def test_aqua_algorithm() -> None: backends: List[Backend] = [AerBackend(), AerStateBackend()] if use_qulacs: backends.append(QulacsBackend()) for b in backends: for comp in (None, b.default_compilation_pass()): if use_qulacs and type(b) == QulacsBackend and comp is None: continue tb = TketBackend(b, comp) ora = TruthTableOracle(bitmaps="01100110") alg = BernsteinVazirani(oracle=ora, quantum_instance=tb) result = alg.run() assert result["result"] == "011" alg = DeutschJozsa(oracle=ora, quantum_instance=tb) result = alg.run() assert result["result"] == "balanced" ora = TruthTableOracle(bitmaps="11111111") alg = DeutschJozsa(oracle=ora, quantum_instance=tb) result = alg.run() assert result["result"] == "constant"
def test_statevector() -> None: b = AerStateBackend() circ = Circuit(3, name="test") circ.H(2) circ.X(0) circ.H(0) circ.CX(0, 1) circ.CZ(1, 2) circ.Sdg(0) circ.Tdg(1) circ.Z(1) circ.T(2) circ.Rx(0.3333, 1) circ.Rz(0.3333, 1) zxcirc = tk_to_pyzx(circ) assert zxcirc.name == circ.name b.compile_circuit(circ) state = b.get_state(circ) circ2 = pyzx_to_tk(zxcirc) assert circ2.name == circ.name b.compile_circuit(circ2) state2 = b.get_state(circ2) assert np.allclose(state, state2, atol=1e-10)
QubitPauliString( [Qubit(0), Qubit(1), Qubit(2), Qubit(3), Qubit(4)], [Pauli.Y, Pauli.Z, Pauli.X, Pauli.X, Pauli.Y], ): 0.4, QubitPauliString([Qubit(0), Qubit(1)], [Pauli.X, Pauli.X]): 0.2, }) shots_result = get_operator_expectation_value(c, op, shots_backend, n_shots) print(shots_result) # The result should be around 0.1, although as the shot simulator is stochastic this will be inexact. Let's test to check what the exact result should be using the statevector simulator: state_backend = AerStateBackend() state_result = get_operator_expectation_value(c, op, state_backend) print(state_result) # Now we can introduce measurement reduction. First we need to choose a strategy: from pytket.partition import PauliPartitionStrat # This first one only performs measurements on simultaneous Pauli operators when there is no cost incurred to do so. strat = PauliPartitionStrat.NonConflictingSets shots_result = get_operator_expectation_value(c, op, shots_backend, n_shots, strat) print(shots_result) # The other strategy we use groups together arbitrary Pauli operators, with the condition that all Pauli operators within a group commute. For an input circuit with $n$ qubits, our method requires the addition of up to $\frac{n(n-1)}{2}$ $CX$ gates to "diagonalise" the Pauli operators, although in practice we find that our techniques tend to give far lower gate overhead than this bound. We describe the procedure in an upcoming paper.
def test_swaps_basisorder() -> None: # Check that implicit swaps can be corrected irrespective of BasisOrder b = AerStateBackend() c = Circuit(4) c.X(0) c.CX(0, 1) c.CX(1, 0) c.CX(1, 3) c.CX(3, 1) c.X(2) cu = CompilationUnit(c) CliffordSimp(True).apply(cu) c1 = cu.circuit assert c1.n_gates_of_type(OpType.CX) == 2 b.compile_circuit(c) b.compile_circuit(c1) handles = b.process_circuits([c, c1]) s_ilo = b.get_state(c1, basis=BasisOrder.ilo) correct_ilo = b.get_state(c, basis=BasisOrder.ilo) assert np.allclose(s_ilo, correct_ilo) s_dlo = b.get_state(c1, basis=BasisOrder.dlo) correct_dlo = b.get_state(c, basis=BasisOrder.dlo) assert np.allclose(s_dlo, correct_dlo) qbs = c.qubits for result in b.get_results(handles): assert (result.get_state([qbs[1], qbs[2], qbs[3], qbs[0]]).real.tolist().index(1.0) == 6) assert (result.get_state([qbs[2], qbs[1], qbs[0], qbs[3]]).real.tolist().index(1.0) == 9) assert (result.get_state([qbs[2], qbs[3], qbs[0], qbs[1]]).real.tolist().index(1.0) == 12) bu = AerUnitaryBackend() u_ilo = bu.get_unitary(c1, basis=BasisOrder.ilo) correct_ilo = bu.get_unitary(c, basis=BasisOrder.ilo) assert np.allclose(u_ilo, correct_ilo) u_dlo = bu.get_unitary(c1, basis=BasisOrder.dlo) correct_dlo = bu.get_unitary(c, basis=BasisOrder.dlo) assert np.allclose(u_dlo, correct_dlo)
# Start timer start = time.time() circ = Circuit(nb_qubits) h = 1.0 Jz = 1.0 for i in range(nb_steps): # Using Heisenberg Hamiltonian: for q in range(nb_qubits): circ.add_pauliexpbox(PauliExpBox([Pauli.X], -h * step_size), [q]) for q in range(nb_qubits - 1): circ.add_pauliexpbox( PauliExpBox([Pauli.Z, Pauli.Z], -Jz * step_size), [q, q + 1]) # Compile to gates backend = AerStateBackend() circ = backend.get_compiled_circuit(circ) # Apply optimization FullPeepholeOptimise().apply(circ) end = time.time() data.append(end - start) print('n_qubits =', nb_qubits, '; Elapsed time =', mean(data), '+/-', stdev(data), '[secs]') # for com in circ: # equivalently, circ.get_commands() # print(com.op, com.op.type, com.args)