Exemplo n.º 1
0
def transform_interaction_operator(transformation: str,
                                   input_operator: Union[str,
                                                         SymbolicOperator]):
    """Transform an interaction operator through either the Bravyi-Kitaev or Jordan-Wigner transformations.
    The results are serialized into a JSON under the files: "transformed-operator.json" and "timing.json"

    ARGS:
        transformation (str): The transformation to use. Either "Jordan-Wigner" or "Bravyi-Kitaev"
        input_operator (Union[str, SymbolicOperator]): The interaction operator to transform
    """
    if isinstance(input_operator, str):
        input_operator = load_interaction_operator(input_operator)

    if transformation == "Jordan-Wigner":
        transformation = jordan_wigner
    elif transformation == "Bravyi-Kitaev":
        input_operator = get_fermion_operator(input_operator)
        transformation = bravyi_kitaev
    else:
        raise RuntimeError("Unrecognized transformation ", transformation)

    start_time = time.time()
    transformed_operator = transformation(input_operator)
    walltime = time.time() - start_time

    save_qubit_operator(transformed_operator, "transformed-operator.json")
    save_timing(walltime, "timing.json")
Exemplo n.º 2
0
def get_local_zero_state_operator(number_of_qubits: int):
    operator = IsingOperator("")
    for qubit_index in range(number_of_qubits):
        operator -= IsingOperator("Z{}".format(qubit_index),
                                  1 / (number_of_qubits))

    save_qubit_operator(operator, "qubit-operator.json")
Exemplo n.º 3
0
def interpolate_qubit_operators(
    reference_qubit_operator: Union[InteractionOperator, str],
    target_qubit_operator: Union[InteractionOperator, str],
    epsilon: float = 0.5,
):
    """Produce a qubit operator which is the interpolation of two operators through the
    function: epsilon * target_qubit_operator + (1.0 - epsilon) *
    reference_qubit_operator.

    Outputs are serialized to JSON under the file: "qubit-operator.json"

    Args:
        reference_qubit_operator: The initial operator
        target_qubit_operator: The target operator
        epsilon: The parameterization between the two operators. Default value is 0.5
    """
    reference_qubit_operator = load_qubit_operator(reference_qubit_operator)
    target_qubit_operator = load_qubit_operator(target_qubit_operator)

    if epsilon > 1.0 or epsilon < 0.0:
        raise ValueError("epsilon must be in the range [0.0, 1.0]")

    output_qubit_operator = (
        epsilon * target_qubit_operator + (1.0 - epsilon) * reference_qubit_operator
    )

    save_qubit_operator(output_qubit_operator, "qubit-operator.json")
Exemplo n.º 4
0
def create_one_qubit_operator(x_coeff: float, y_coeff: float, z_coeff: float,
                              constant: float) -> None:

    qubit_operator = (x_coeff * QubitOperator("X0") +
                      y_coeff * QubitOperator("Y0") +
                      z_coeff * QubitOperator("Z0") + constant * QubitOperator(
                          ()))
    save_qubit_operator(qubit_operator, "qubit_operator.json")
Exemplo n.º 5
0
def get_one_qubit_hydrogen_hamiltonian(
    interaction_operator: Union[InteractionOperator, str]
):
    """Generate a one qubit H2 hamiltonian from a corresponding interaction operator.

    Original H2 hamiltonian will be reduced to a 2 x 2 matrix defined on a subspace
    spanned by |0011> and |1100> and expanded in terms of I, X, Y, and Z matrices

    Args:
        interaction_operator: The input interaction operator
    """
    if isinstance(interaction_operator, str):
        interaction_operator = load_interaction_operator(interaction_operator)

    fermion_h = get_fermion_operator(interaction_operator)

    # H00
    H00 = normal_ordered(FermionOperator("0 1") * fermion_h * FermionOperator("1^ 0^"))
    H00 = H00.terms[()]

    # H11
    H11 = normal_ordered(FermionOperator("2 3") * fermion_h * FermionOperator("3^ 2^"))
    H11 = H11.terms[()]

    # H10
    H10 = normal_ordered(FermionOperator("2 3") * fermion_h * FermionOperator("1^ 0^"))
    H10 = H10.terms[()]

    # H01
    H01 = np.conj(H10)

    one_qubit_h_matrix = np.array([[H00, H01], [H10, H11]])
    pauli_x = np.array([[0.0, 1.0], [1.0, 0.0]])
    pauli_y = np.array([[0.0, -1.0j], [1.0j, 0.0]])
    pauli_z = np.array([[1.0, 0.0], [0.0, -1.0]])

    r_id = 0.5 * np.trace(one_qubit_h_matrix)
    r_x = 0.5 * np.trace(one_qubit_h_matrix @ pauli_x)
    r_y = 0.5 * np.trace(one_qubit_h_matrix @ pauli_y)
    r_z = 0.5 * np.trace(one_qubit_h_matrix @ pauli_z)

    one_qubit_h = (
        r_id * QubitOperator("")
        + r_x * QubitOperator("X0")
        + r_y * QubitOperator("Y0")
        + r_z * QubitOperator("Z0")
    )

    save_qubit_operator(one_qubit_h, "qubit-operator.json")
Exemplo n.º 6
0
def get_one_qubit_hydrogen_hamiltonian(
        interaction_operator: Union[InteractionOperator, str]):
    """Generate a one qubit H2 hamiltonian from a corresponding interaction operator. 

    Original H2 hamiltonian will be reduced to a 2 x 2 matrix defined on a subspace spanned
    by |0011> and |1100> and expanded in terms of I, X, Y, and Z matrices

    ARGS:
        interaction_operator (Union[InteractionOperator, str]): The input interaction operator
    """
    if isinstance(interaction_operator, str):
        interaction_operator = load_interaction_operator(interaction_operator)

    fermion_h = get_fermion_operator(interaction_operator)

    # H00
    H00 = normal_ordered(
        FermionOperator('0 1') * fermion_h * FermionOperator('1^ 0^'))
    H00 = H00.terms[()]

    # H11
    H11 = normal_ordered(
        FermionOperator('2 3') * fermion_h * FermionOperator('3^ 2^'))
    H11 = H11.terms[()]

    # H10
    H10 = normal_ordered(
        FermionOperator('2 3') * fermion_h * FermionOperator('1^ 0^'))
    H10 = H10.terms[()]

    # H01
    H01 = np.conj(H10)

    one_qubit_h_matrix = np.array([[H00, H01], [H10, H11]])
    pauli_x = np.array([[0., 1.], [1., 0.]])
    pauli_y = np.array([[0., -1.j], [1.j, 0.]])
    pauli_z = np.array([[1., 0.], [0., -1.]])

    r_id = 0.5 * np.trace(one_qubit_h_matrix)
    r_x = 0.5 * np.trace(one_qubit_h_matrix @ pauli_x)
    r_y = 0.5 * np.trace(one_qubit_h_matrix @ pauli_y)
    r_z = 0.5 * np.trace(one_qubit_h_matrix @ pauli_z)

    one_qubit_h = r_id * QubitOperator('') + r_x * QubitOperator(
        'X0') + r_y * QubitOperator('Y0') + r_z * QubitOperator('Z0')

    save_qubit_operator(one_qubit_h, 'qubit-operator.json')
Exemplo n.º 7
0
def create_random_qubitop(nqubits: int, nterms: int):

    output_qubit_operator = _create_random_qubitop(nqubits, nterms)

    save_qubit_operator(output_qubit_operator, "qubit-operator.json")
Exemplo n.º 8
0
def get_graph_partition_hamiltonian(graph, scale_factor=1.0, offset=0.0):
    graph_object = load_graph(graph)
    hamiltonian = GraphPartitioning().get_hamiltonian(
        graph_object, scale_factor=scale_factor, offset=offset)
    save_qubit_operator(hamiltonian, "hamiltonian.json")
Exemplo n.º 9
0
def get_maxcut_hamiltonian(graph, scale_factor=1.0, offset=0.0):
    graph_object = load_graph(graph)
    hamiltonian = MaxCut().get_hamiltonian(graph_object,
                                           scale_factor=scale_factor,
                                           offset=offset)
    save_qubit_operator(hamiltonian, "hamiltonian.json")
Exemplo n.º 10
0
def get_maxcut_hamiltonian(graph, scaling=1.0, shifted=False):
    graph_object = load_graph(graph)
    hamiltonian = _get_maxcut_hamiltonian(
        graph_object, scaling=scaling, shifted=shifted
    )
    save_qubit_operator(hamiltonian, "hamiltonian.json")