Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide option to assume zero initial state in ansatz generation #10

Merged
merged 4 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions qiskit_addon_aqc_tensor/ansatz_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,12 @@ def _nonidle_qubits(qc: QuantumCircuit, /):
}


def generate_ansatz_from_circuit(qc: QuantumCircuit, /) -> tuple[QuantumCircuit, list[float]]:
def generate_ansatz_from_circuit(
qc: QuantumCircuit,
/,
*,
qubits_initially_zero=False,
) -> tuple[QuantumCircuit, list[float]]:
"""Generate an ansatz from the two-qubit connectivity structure of a circuit."""
# FIXME: handle classical bits, measurements, resets, and barriers. maybe
# conditions too?
Expand All @@ -138,7 +143,14 @@ def set_zxz_params_from_mat(q: int, mat) -> None:
# Following the variable convention at
# https://docs.quantum.ibm.com/api/qiskit/qiskit.synthesis.OneQubitEulerDecomposer
theta, phi, lamb = decomposer.angles(mat)
for j, r in zip(free_params[q], (lamb, theta, phi)):
fp = free_params[q]
values: tuple[float, ...] = lamb, theta, phi
if len(fp) == 2:
# Must be initial gate, where the Z rotation has been dropped.
# This makes sense if we assume the input state to this ZXZ block
# is |0>.
values = values[1:]
for j, r in zip(fp, values):
initial_params[j] = r

def perform_separation(q0: int, q1: int):
Expand All @@ -164,8 +176,10 @@ def perform_separation(q0: int, q1: int):

active_qubits = sorted([qc.find_bit(q)[0] for q in _nonidle_qubits(qc)])
for q in active_qubits:
params, free_params[q] = _allocate_parameters(param_vec, 3)
initial_params.extend([np.nan] * 3)
params, free_params[q] = _allocate_parameters(param_vec, 2 if qubits_initially_zero else 3)
initial_params.extend([np.nan] * len(params))
if qubits_initially_zero:
params.insert(0, 0.0)
ansatz.append(ZXZ(params), (q,))
singles[q] = []

Expand Down
12 changes: 10 additions & 2 deletions test/test_ansatz_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,28 @@
import pytest
from qiskit.circuit import QuantumCircuit
from qiskit.circuit.random import random_circuit
from qiskit.quantum_info import Operator, process_fidelity
from qiskit.quantum_info import Operator, Statevector, process_fidelity, state_fidelity

from qiskit_addon_aqc_tensor import generate_ansatz_from_circuit
from qiskit_addon_aqc_tensor.ansatz_generation import KAK


def test_ansatz_from_random_circuit():
def test_ansatz_from_random_circuit_process_fidelity():
qc = random_circuit(6, 4, max_operands=2)
ansatz, initial_params = generate_ansatz_from_circuit(qc)
ansatz.assign_parameters(initial_params, inplace=True)
fidelity = process_fidelity(Operator(ansatz), Operator(qc))
assert fidelity == pytest.approx(1)


def test_ansatz_from_random_circuit_state_fidelity():
qc = random_circuit(6, 4, max_operands=2)
ansatz, initial_params = generate_ansatz_from_circuit(qc, qubits_initially_zero=True)
ansatz.assign_parameters(initial_params, inplace=True)
fidelity = state_fidelity(Statevector(ansatz), Statevector(qc))
assert fidelity == pytest.approx(1)


def test_ansatz_fails_given_three_qubit_gate():
qc = QuantumCircuit(3)
qc.h(0)
Expand Down
4 changes: 3 additions & 1 deletion test/test_aqc_workflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ def test_basic_workflow(available_backend_fixture, circuit_pair):
target_mps = tensornetwork_from_circuit(target_circuit, simulator_settings)
good_mps = tensornetwork_from_circuit(good_circuit, simulator_settings)
initial_fidelity = abs(compute_overlap(good_mps, target_mps)) ** 2
ansatz, initial_parameters = generate_ansatz_from_circuit(good_circuit)
ansatz, initial_parameters = generate_ansatz_from_circuit(
good_circuit, qubits_initially_zero=True
)
objective = OneMinusFidelity(target_mps, ansatz, simulator_settings)
result = minimize(
objective,
Expand Down