Esempio n. 1
0
def interference(repeat_count: int, starting_state: int) -> dict[tuple[int, ...], int]:
    """Creates a superposition and applies interference on a qubit in a specified state a specified number of times.

    Args:
        repeat_count (int): The number of times to repeat the quantum circuit.
        starting_state (int): The state to set the qubit to prior to running the quantum circuit.

    Returns:
        dict[tuple[int, ...], int]: The measurement result counts.
    """
    # Set up quantum circuit with 1 qubit and 1 bit.
    # Set qubit to desired starting state.
    # If the starting state should be 1, apply the X gate.
    circuit = Circuit(1, 1)
    if starting_state == 1:
        circuit.X(0)
    
    # Apply Hadamard gate to create a superposition.
    circuit.H(0)

    # Apply Hadamard gate to cause interference to restore qubit to its starting state.
    circuit.H(0)

    # Measure the qubit and save the result into a classical bit.
    circuit.Measure(0, 0)

    # Initialise backend and run circuit a specified number of times.
    backend.compile_circuit(circuit)
    job = backend.process_circuit(circuit, n_shots=repeat_count)

    # Get counts of qubit measurement results.
    result_counts = backend.get_result(job).get_counts()
    return result_counts
Esempio n. 2
0
def ucc(params):
    ansatz = Circuit(4)
    ansatz.X(1).X(3)
    add_excitation(ansatz, singles_a, params[0])
    add_excitation(ansatz, singles_b, params[1])
    add_excitation(ansatz, doubles, params[2])
    DecomposeBoxes().apply(ansatz)
    return ansatz
Esempio n. 3
0
def test_basisorder() -> None:
    c = Circuit(2)
    c.X(1)
    b = MyBackend()
    b.compile_circuit(c)
    assert np.allclose(b.get_state(c), np.asarray([0, 1, 0, 0]))
    assert np.allclose(b.get_state(c, basis=BasisOrder.dlo),
                       np.asarray([0, 0, 1, 0]))
Esempio n. 4
0
def generate_hf_wavefunction_circuit(n_qubit: int, n_alpha_electron: int, n_beta_electron: int):
   """
   Args:
       n_qubit (int):
       n_alpha_electron (int):
       n_beta_electron (int):
   """
   circuit = Circuit(n_qubit)

   for i in range(n_alpha_electron):
       idx_alpha = 2 * i
       circuit.X(idx_alpha)

   for i in range(n_beta_electron):
       idx_beta = 2 * i + 1
       circuit.X(idx_beta)

   return circuit
Esempio n. 5
0
def cnot_unitary_to_circuit(u, n_qubits):
    # desc = greedy(matrix_to_function(u, n_qubits), n_qubits)  # Doesn't converge, don't run this
    desc = decompose_unitary(u)
    c = Circuit(n_qubits)
    for d in desc:
        if d[0] == "N":
            c.X(int(d[2:]))
        elif d[0] == "C":
            c.CX(int(d.split(":")[1]), int(d.split(":")[2]))
    return c
Esempio n. 6
0
def test_sampler_basisorder() -> None:
    c = Circuit(2, 2)
    c.X(1)
    c.measure_all()
    b = MySampler()
    b.compile_circuit(c)
    assert b.get_counts(c, n_shots=10, seed=0) == {(0, 1): 10}
    assert b.get_counts(c, n_shots=10, seed=0, basis=BasisOrder.dlo) == {
        (1, 0): 10
    }
Esempio n. 7
0
def ucc(params):
    ansatz = Circuit(4)
    # Set initial reference state
    ansatz.X(1).X(3)
    # Evolve by excitations
    for term, coeff in singles_a.items():
        add_operator_term(ansatz, term, coeff * params[0])
    for term, coeff in singles_b.items():
        add_operator_term(ansatz, term, coeff * params[1])
    for term, coeff in doubles.items():
        add_operator_term(ansatz, term, coeff * params[2])
    return ansatz
Esempio n. 8
0
def h2_JW_sto3g_ansatz():
    symbols = [Symbol("s0"), Symbol("s1"), Symbol("s2")]
    ansatz = Circuit(4)
    # Initialise in Hartree-Fock state
    ansatz.X(0)
    ansatz.X(1)
    # Single excitations
    ansatz.add_pauliexpbox(
        PauliExpBox([Pauli.X, Pauli.Z, Pauli.Y, Pauli.I], -symbols[0]),
        [0, 1, 2, 3])
    ansatz.add_pauliexpbox(
        PauliExpBox([Pauli.Y, Pauli.Z, Pauli.X, Pauli.I], symbols[0]),
        [0, 1, 2, 3])
    ansatz.add_pauliexpbox(
        PauliExpBox([Pauli.I, Pauli.X, Pauli.Z, Pauli.Y], -symbols[1]),
        [0, 1, 2, 3])
    ansatz.add_pauliexpbox(
        PauliExpBox([Pauli.I, Pauli.Y, Pauli.Z, Pauli.X], symbols[1]),
        [0, 1, 2, 3])
    # Double excitations
    ansatz.add_pauliexpbox(
        PauliExpBox([Pauli.X, Pauli.X, Pauli.X, Pauli.Y], -symbols[2]),
        [0, 1, 2, 3])
    ansatz.add_pauliexpbox(
        PauliExpBox([Pauli.X, Pauli.X, Pauli.Y, Pauli.X], -symbols[2]),
        [0, 1, 2, 3])

    ansatz.add_pauliexpbox(
        PauliExpBox([Pauli.X, Pauli.Y, Pauli.X, Pauli.X], symbols[2]),
        [0, 1, 2, 3])
    ansatz.add_pauliexpbox(
        PauliExpBox([Pauli.Y, Pauli.X, Pauli.X, Pauli.X], symbols[2]),
        [0, 1, 2, 3])

    ansatz.add_pauliexpbox(
        PauliExpBox([Pauli.X, Pauli.Y, Pauli.Y, Pauli.Y], -symbols[2]),
        [0, 1, 2, 3])
    ansatz.add_pauliexpbox(
        PauliExpBox([Pauli.Y, Pauli.X, Pauli.Y, Pauli.Y], -symbols[2]),
        [0, 1, 2, 3])

    ansatz.add_pauliexpbox(
        PauliExpBox([Pauli.Y, Pauli.Y, Pauli.X, Pauli.Y], symbols[2]),
        [0, 1, 2, 3])
    ansatz.add_pauliexpbox(
        PauliExpBox([Pauli.Y, Pauli.Y, Pauli.Y, Pauli.X], symbols[2]),
        [0, 1, 2, 3])
    # Synthesise structures into primitive gates
    DecomposeBoxes().apply(ansatz)
    return ansatz, symbols
Esempio n. 9
0
# Bell state between Alice and Bob:

qtel.H(alice[1])
qtel.CX(alice[1], bob[0])

# Bell measurement of Alice's qubits:

qtel.CX(alice[0], alice[1])
qtel.H(alice[0])
qtel.Measure(alice[0], data[0])
qtel.Measure(alice[1], data[1])

# Correction of Bob's qubit:

qtel.X(bob[0], condition_bits=[data[0], data[1]], condition_value=2)
qtel.X(bob[0], condition_bits=[data[0], data[1]], condition_value=3)
qtel.Z(bob[0], condition_bits=[data[0], data[1]], condition_value=1)
qtel.Z(bob[0], condition_bits=[data[0], data[1]], condition_value=3)

# So to demonstrate the Entanglement Swapping protocol, we just need to run this on one side of a Bell pair.

es = Circuit()
ava = es.add_q_register("a", 1)
bella = es.add_q_register("b", 2)
charlie = es.add_q_register("c", 1)
data = es.add_c_register("d", 2)

# Bell state between Ava and Bella:

es.H(ava[0])
Esempio n. 10
0
print('*** Hello, Quantum! - Phase Kickback (tket) ***')

repeat_count = 1000

# Set up cirbuit with 2 qubits and 2 classical bits.
circuit = Circuit(2, 2)

# Apply Hadamard gate to control qubit to create a superposition in the
# (+)-superposition state.
circuit.H(0)

# Apply X gate to target qubit to set it to the 1 state.
# Apply Hadamard gate to target qubit to create a superposition in the
# (-)-superposition state.
circuit.X(1)
circuit.H(1)

# Apply CNOT gate to target qubit using the control qubit to trigger phase
# kickback onto the control qubit.
circuit.CX(0, 1)

# Measure qubits in the Hadamard (X) basis.
measure_x(circuit, 0, 0)
measure_x(circuit, 1, 1)

# Initialise backend and run circuit a specified number of times.
backend.compile_circuit(circuit)
job = backend.process_circuit(circuit, n_shots=repeat_count)

# Get counts of qubit measurement results.
Esempio n. 11
0
#
# With the number of simulator `Backend`s available across the `pytket` extension modules, we are often asked why to use one over another. Surely, any two simulators are equivalent if they are able to sample the circuits in the same way, right? Not quite. In this notebook we go through each of the simulators in turn and describe what sets them apart from others and how to make use of any unique features.
#
# But first, to demonstrate the significant overlap in functionality, we'll just give some examples of common usage for different types of backends.

# ## Sampling simulator usage

from pytket import Circuit
from pytket.extensions.qiskit import AerBackend

# Define a circuit:

c = Circuit(3, 3)
c.Ry(0.7, 0)
c.CX(0, 1)
c.X(2)
c.measure_all()

# Run on the backend:

backend = AerBackend()
backend.compile_circuit(c)
handle = backend.process_circuit(c, n_shots=2000)
counts = backend.get_result(handle).get_counts()
print(counts)

# ## Statevector simulator usage

from pytket import Circuit
from pytket.extensions.qiskit import AerStateBackend