# Linear System Question

**URL:** <https://discuss.pennylane.ai/t/linear-system-question/9349>\
**Category:** Demos\
**Created:** [May 22, 2026, 3:26am UTC](https://discuss.pennylane.ai/t/linear-system-question/9349 "2026-05-22T03:26:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![movingwaters](https://avatars.discourse-cdn.com/v4/letter/m/edb3f5/32.png) [@movingwaters](https://discuss.pennylane.ai/u/movingwaters)\
**Post date:** [May 22, 2026, 3:26am UTC](https://discuss.pennylane.ai/t/linear-system-question/9349/1 "2026-05-22T03:26:24Z")

</div>

Hello,

I’ve seen topics regarding solving systems of linear equations and formulas for Ax=b with quantum algorithms that are solving for the x if we have A and b. I’m wondering if we are just wanting to apply x on a matrix A, how would we go about that in a quantum algorithm?

Thank you

---

<div class="post-metadata">

**Author:** ![CatalinaAlbornoz](https://yyz2.discourse-cdn.com/flex012/user_avatar/discuss.pennylane.ai/catalinaalbornoz/32/1196_2.png) [@CatalinaAlbornoz](https://discuss.pennylane.ai/u/CatalinaAlbornoz)\
**Post date:** [May 27, 2026, 12:15am UTC](https://discuss.pennylane.ai/t/linear-system-question/9349/2 "2026-05-27T00:15:41Z")

</div>

Hi @movingwaters , welcome to the Forum 👋

This is a really good question! The answer depends on whether or not your matrix A is unitary.

First, remember that applying a gate to a quantum state is applying a _matrix_ to that state, which itself is represented as a _vector_.

You would start with encoding your vector x into the quantum computer, for example using [qp.AmplitudeEmbedding](https://docs.pennylane.ai/en/stable/code/api/pennylane.AmplitudeEmbedding.html).

If the matrix A is _unitary_ then you can directly implement it as a quantum gate. If the matrix’s dimensions are a power of two you can add it to your circuit with [qp.QubitUnitary](https://docs.pennylane.ai/en/stable/code/api/pennylane.QubitUnitary.html).

If your matrix A is non-unitary (general case) then you can use _block encodings_. This means embedding A as a sub-block of a larger unitary U, acting on a larger system which includes ancilla qubits. In practice we actually embed A/\alpha (where \alpha \geq |A|) so that the final unitary is actually unitary. You can learn more about Block Encodings and how to use them in PennyLane in the [PennyLane Codebook](https://pennylane.ai/codebook/hamiltonian-simulation/lcus).

The procedure is:

1. Prepare ancilla qubits in |0\rangle
2. Encode |x\rangle in the system register
3. Apply the unitary U
4. Measure the ancilla.

If you get |0\rangle, the system register holds A|x\rangle / \alpha (up to normalization)

The success probability is |A|x\rangle|^2 / \alpha^2, so [amplitude amplification](https://pennylane.ai/codebook/grovers-algorithm/amplitude-amplification) can boost this.

Here’s a full code example you can use.

```auto
import pennylane as qp
import numpy as np

# Define your matrix A and vector x
A = np.array([
    [0.3, 0.5],
    [0.1, 0.4]
])

x = np.array([0.6, 0.8])

# Normalize x (required for AmplitudeEmbedding)
x_norm = np.linalg.norm(x)
x_normalized = x / x_norm

# Create the block encoding operator
block_op = qp.BlockEncode(A, wires=[0, 1])

# Check PennyLane's auto-normalization factor
norm_factor = block_op.hyperparameters["norm"]
effective_norm = max(norm_factor, 1.0)

# Build the circuit
dev = qp.device("default.qubit")

@qp.qnode(dev)
def apply_A_to_x():
    # Wire 0 = ancilla, Wire 1 = system
    qp.AmplitudeEmbedding(x_normalized, wires=1)
    qp.BlockEncode(A, wires=[0, 1])
    return qp.state()

# Execute and post-select
statevector = apply_A_to_x()
post_selected = statevector[:2] # ancilla = |0> subspace

# Recover A @ x
Ax_result = effective_norm * x_norm * post_selected
print(f"Quantum result: {Ax_result.real}") # [0.58, 0.38]
print(f"Classical A@x: {(A @ x).real}") # [0.58, 0.38]

```

I hope this helps!

---

<div class="post-metadata">

**Author:** ![movingwaters](https://avatars.discourse-cdn.com/v4/letter/m/edb3f5/32.png) [@movingwaters](https://discuss.pennylane.ai/u/movingwaters)\
**Post date:** [May 28, 2026, 2:05pm UTC](https://discuss.pennylane.ai/t/linear-system-question/9349/3 "2026-05-28T14:05:25Z")

</div>

Hello,

Thank you for the explanation and example, it makes a lot of sense and is really cool to see how it all works!
