Skip to content

Commit

Permalink
Provide option to assume zero initial state in ansatz generation (#10)
Browse files Browse the repository at this point in the history
* Provide option to assume zero initial state in ansatz generation

The ansatz begins with ZXZ decompositions, but the first RZGate
on each qubit has no effect if acting on the initial state.  So,
this adds an option to remove that parameter on each qubit.

It's left as an option since it does not make sure to do in the case
of unitary AQC.

I'm definitely open to discussing the name of the option.

TODO

- [ ] add new argument to docstring

* I like these test names better

* Update name to `qubits_initially_zero`

* Use `qubits_initially_zero=True` in workflow test
  • Loading branch information
garrison authored Nov 4, 2024
1 parent da45c0d commit ca9f741
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
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

0 comments on commit ca9f741

Please sign in to comment.