# How to create a single shared parameter for multiple gates within a circuit directly using Yao.jl?

**URL:** <https://discourse.julialang.org/t/how-to-create-a-single-shared-parameter-for-multiple-gates-within-a-circuit-directly-using-yao-jl/104648>\
**Category:** Performance\
**Tags:** yao, parameters, julia\
**Created:** [October 6, 2023, 7:01am UTC](https://discourse.julialang.org/t/how-to-create-a-single-shared-parameter-for-multiple-gates-within-a-circuit-directly-using-yao-jl/104648 "2023-10-06T07:01:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nethaji8](https://avatars.discourse-cdn.com/v4/letter/n/58f4c7/32.png) [@nethaji8](https://discourse.julialang.org/u/nethaji8)\
**Post date:** [October 6, 2023, 7:01am UTC](https://discourse.julialang.org/t/how-to-create-a-single-shared-parameter-for-multiple-gates-within-a-circuit-directly-using-yao-jl/104648/1 "2023-10-06T07:01:57Z")

</div>

I am trying to parameterize a circuit using a single variable `θ` in the code below but the output is:  
` Number of parameters in the circuit: 4`  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/8/a8ad987f2c02054345611f0c2f8f8170238711e8.png).

Can someone please help me create a single shared parameter for multiple gates within a circuit directly?

```julia
using Yao
using YaoPlots

# Define the number of qubits
n = 5

# Define a single parameter for all Rz gates
θ = π/4 # replace this with your desired angle

# Create a chain block to hold our circuit
circuit = chain(n)

# Apply controlled Rz gates with the shared parameter gate
for i in 1:n-1
    push!(circuit, control(i, i+1 => Rz(θ)))
end

# Check the number of parameters in the circuit
println("Number of parameters in the circuit: ", length(parameters(circuit)))
plot(circuit)

```

---

<div class="post-metadata">

**Author:** ![Roger-luo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roger-luo/32/3399_2.png) [@Roger-luo](https://discourse.julialang.org/u/Roger-luo)\
**Post date:** [October 6, 2023, 1:43pm UTC](https://discourse.julialang.org/t/how-to-create-a-single-shared-parameter-for-multiple-gates-within-a-circuit-directly-using-yao-jl/104648/2 "2023-10-06T13:43:29Z")

</div>

Hi, this is one limitation of Yao currently, you need to do either the following

- sum up the gradient yourself (recommend in simple cases)
- use a general-purpose AD engine e.g Zygote

there is [an example](https://github.com/QuantumBFS/Yao.jl/blob/cf5b36da79554afff5fc1a40f9b450e755f5fdd2/lib/YaoBlocks/test/autodiff/chainrules_patch.jl#L67) in our test for Zygote if you want to do the second approach

---

<div class="post-metadata">

**Author:** ![1115](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1115/32/4465_2.png) [@1115](https://discourse.julialang.org/u/1115)\
**Post date:** [October 6, 2023, 1:51pm UTC](https://discourse.julialang.org/t/how-to-create-a-single-shared-parameter-for-multiple-gates-within-a-circuit-directly-using-yao-jl/104648/3 "2023-10-06T13:51:11Z")

</div>

For training with shared parameters, Yao assume the parameter sharing is automatically handled by your AD engine such as Zygote. Here is an example: [https://github.com/QuantumBFS/QuAlgorithmZoo.jl/blob/master/examples/PortZygote/shared\_parameters.jl](https://github.com/QuantumBFS/QuAlgorithmZoo.jl/blob/master/examples/PortZygote/shared_parameters.jl)

If you want a real parameter sharing, I would suggest using symbol engine:

```julia
julia> using Yao, SymEngine

julia> @vars θ
(θ,)

julia> # Create a chain block to hold our circuit
       circuit = chain(n)

       # Apply controlled Rz gates with the shared parameter gate
nqubits: 5
chain

julia> for i in 1:n-1
           push!(circuit, control(i, i+1 => Rz(θ)))
       end

julia> circuit
nqubits: 5
chain
├─ control(1)
│ └─ (2,) rot(Z, θ)
├─ control(2)
│ └─ (3,) rot(Z, θ)
├─ control(3)
│ └─ (4,) rot(Z, θ)
└─ control(4)
   └─ (5,) rot(Z, θ)

julia> subs(circuit, θ=>0.4)
nqubits: 5
chain
├─ control(1)
│ └─ (2,) rot(Z, 0.4)
├─ control(2)
│ └─ (3,) rot(Z, 0.4)
├─ control(3)
│ └─ (4,) rot(Z, 0.4)
└─ control(4)
   └─ (5,) rot(Z, 0.4)

```

---

<div class="post-metadata">

**Author:** ![nethaji8](https://avatars.discourse-cdn.com/v4/letter/n/58f4c7/32.png) [@nethaji8](https://discourse.julialang.org/u/nethaji8)\
**Post date:** [October 7, 2023, 5:10am UTC](https://discourse.julialang.org/t/how-to-create-a-single-shared-parameter-for-multiple-gates-within-a-circuit-directly-using-yao-jl/104648/4 "2023-10-07T05:10:32Z")

</div>

Dear @Roger-luo and @1115 thank you for your response 🙂 .
