Skip to content

Hardware Efficient Ansatz

In NISQ-era devices, to reduce noise, use short and hardware-fitting quantum circuits.

Hardware-efficient ansatz is generated to fit a specific hardware with given connectivity map and basis gates [1] . The hardware-efficient ansatz is built from a layer of single qubit gates followed by a layer of entangling two qubit gates. Repeating this structure allows a more expressive ansatz at the cost of increased depth.

Syntax

HEAParameters:

  • num_qubits: int – Number of qubits in the ansatz.
  • connectivity_map: List[Tuple[int]] – Hardware connectivity map, in the form [ [x0, x1], [x1, x2],...].
  • reps: int – Number of layers in the ansatz.
  • one_qubit_gates: List[str] – List of gates for the one qubit gates layer, e.g., ["x", "ry"].
  • two_qubit_gates: List[str] – List of gates for the two qubit gates entangling layer, e.g., ["cx", "cry"].

If the number of qubits is not specified, the number of the qubits from the connectivity map is used. If the connectivity map is not specified, the connectivity map from the model hardware settings is used. If that is also not specified, all qubit pairs are connected.

The allowed one_qubit_gates: '["x", "y", "z", "h", "p", "i", "rx", "ry", "rz", "s", "sdg", "t", "tdg"]'.

The allowed two_qubit_gates: '["cx", "cy", "cz", "ch", "cp", "crx", "cry", "crz", "rxx", "ryy", "rzz", "swap"]'.

Example

This example demonstrates how to generate a hardware-efficient ansatz. It uses a four qubit hardware with full connectivity.

qmod
{
  "functions": [
    {
      "name": "full_hea",
      "param_decls": {
        "num_qubits": {
          "kind": "int"
        },
        "is_parametrized": {
          "kind": "list",
          "element_type": {
            "kind": "int"
          }
        },
        "angle_params": {
          "kind": "list",
          "element_type": {
            "kind": "real"
          }
        },
        "connectivity_map": {
          "kind": "list",
          "element_type": {
            "kind": "list",
            "element_type": {
              "kind": "int"
            }
          }
        },
        "reps": {
          "kind": "int"
        }
      },
      "port_declarations": {
        "x": {
          "name": "x",
          "size": {
            "expr": "num_qubits"
          },
          "direction": "inout"
        }
      },
      "operand_declarations": {
        "operands_1qubit": {
          "name": "operands_1qubit",
          "param_decls": {
            "angle": {
              "kind": "real"
            }
          },
          "port_declarations": {
            "q": {
              "name": "q",
              "size": {
                "expr": "1"
              },
              "direction": "inout"
            }
          },
          "operand_declarations": {},
          "is_list": true
        },
        "operands_2qubit": {
          "name": "operands_2qubit",
          "param_decls": {
            "angle": {
              "kind": "real"
            }
          },
          "port_declarations": {
            "q1": {
              "name": "q1",
              "size": {
                "expr": "1"
              },
              "direction": "inout"
            },
            "q2": {
              "name": "q2",
              "size": {
                "expr": "1"
              },
              "direction": "inout"
            }
          },
          "operand_declarations": {},
          "is_list": true
        }
      },
      "parameters": [],
      "body": [
        {
          "function": "repeat",
          "params": {
            "count": {
              "expr": "reps"
            },
            "port_size": {
              "expr": "num_qubits"
            }
          },
          "function_params": {
            "parameters": [],
            "input_decls": {},
            "output_decls": {}
          },
          "control_states": [],
          "inputs": {
            "qbv": {
              "name": "x"
            }
          },
          "inouts": {},
          "outputs": {
            "qbv": {
              "name": "x"
            }
          },
          "name": "repeat_cs4id_rPIPxs",
          "operands": {
            "iteration": {
              "rename_params": {
                "index": "r"
              },
              "body": [
                {
                  "function": "repeat",
                  "params": {
                    "count": {
                      "expr": "len(operands_1qubit)"
                    },
                    "port_size": {
                      "expr": "num_qubits"
                    }
                  },
                  "function_params": {
                    "parameters": [],
                    "input_decls": {},
                    "output_decls": {}
                  },
                  "control_states": [],
                  "inputs": {
                    "qbv": {
                      "name": "qbv"
                    }
                  },
                  "inouts": {},
                  "outputs": {
                    "qbv": {
                      "name": "qbv"
                    }
                  },
                  "name": "repeat_cs4id_bCZ7Dr",
                  "operands": {
                    "iteration": {
                      "rename_params": {
                        "index": "i1"
                      },
                      "body": [
                        {
                          "function": "repeat",
                          "params": {
                            "count": {
                              "expr": "num_qubits"
                            },
                            "port_size": {
                              "expr": "num_qubits"
                            }
                          },
                          "function_params": {
                            "parameters": [],
                            "input_decls": {},
                            "output_decls": {}
                          },
                          "control_states": [],
                          "inputs": {
                            "qbv": {
                              "name": "qbv"
                            }
                          },
                          "inouts": {},
                          "outputs": {
                            "qbv": {
                              "name": "qbv"
                            }
                          },
                          "name": "repeat_cs4id_QXMBEi",
                          "operands": {
                            "iteration": {
                              "rename_params": {},
                              "body": [
                                {
                                  "function": "if",
                                  "params": {
                                    "condition": {
                                      "expr": "Eq(is_parametrized[i1],1)"
                                    },
                                    "port_size": {
                                      "expr": "num_qubits"
                                    }
                                  },
                                  "function_params": {
                                    "parameters": [],
                                    "input_decls": {},
                                    "output_decls": {}
                                  },
                                  "control_states": [],
                                  "inputs": {},
                                  "inouts": {
                                    "qbv": {
                                      "name": "qbv"
                                    }
                                  },
                                  "outputs": {},
                                  "name": "if_cs4id_60ZSPt",
                                  "operands": {
                                    "then": {
                                      "rename_params": {},
                                      "body": [
                                        {
                                          "function": {
                                            "name": "operands_1qubit",
                                            "index": {
                                              "expr": "i1"
                                            }
                                          },
                                          "params": {
                                            "angle": {
                                              "expr": "angle_params[sum(is_parametrized[0:i1])+len(angle_params)//reps*r+index]"
                                            }
                                          },
                                          "function_params": {
                                            "parameters": [],
                                            "input_decls": {},
                                            "output_decls": {}
                                          },
                                          "control_states": [],
                                          "inputs": {},
                                          "inouts": {
                                            "q": {
                                              "name": "qbv",
                                              "start": {
                                                "expr": "index"
                                              },
                                              "end": {
                                                "expr": "index+1"
                                              }
                                            }
                                          },
                                          "outputs": {},
                                          "name": "operands_1qubit_cs4id_aYQISK",
                                          "operands": {}
                                        }
                                      ]
                                    },
                                    "else": {
                                      "rename_params": {},
                                      "body": [
                                        {
                                          "function": {
                                            "name": "operands_1qubit",
                                            "index": {
                                              "expr": "i1"
                                            }
                                          },
                                          "params": {
                                            "angle": {
                                              "expr": "0"
                                            }
                                          },
                                          "function_params": {
                                            "parameters": [],
                                            "input_decls": {},
                                            "output_decls": {}
                                          },
                                          "control_states": [],
                                          "inputs": {},
                                          "inouts": {
                                            "q": {
                                              "name": "qbv",
                                              "start": {
                                                "expr": "index"
                                              },
                                              "end": {
                                                "expr": "index+1"
                                              }
                                            }
                                          },
                                          "outputs": {},
                                          "name": "operands_1qubit_cs4id_KOmrYs",
                                          "operands": {}
                                        }
                                      ]
                                    }
                                  }
                                }
                              ]
                            }
                          }
                        }
                      ]
                    }
                  }
                },
                {
                  "function": "repeat",
                  "params": {
                    "count": {
                      "expr": "len(operands_2qubit)"
                    },
                    "port_size": {
                      "expr": "num_qubits"
                    }
                  },
                  "function_params": {
                    "parameters": [],
                    "input_decls": {},
                    "output_decls": {}
                  },
                  "control_states": [],
                  "inputs": {
                    "qbv": {
                      "name": "qbv"
                    }
                  },
                  "inouts": {},
                  "outputs": {
                    "qbv": {
                      "name": "qbv"
                    }
                  },
                  "name": "repeat_cs4id_AiySGL",
                  "operands": {
                    "iteration": {
                      "rename_params": {
                        "index": "i2"
                      },
                      "body": [
                        {
                          "function": "repeat",
                          "params": {
                            "count": {
                              "expr": "len(connectivity_map)"
                            },
                            "port_size": {
                              "expr": "num_qubits"
                            }
                          },
                          "function_params": {
                            "parameters": [],
                            "input_decls": {},
                            "output_decls": {}
                          },
                          "control_states": [],
                          "inputs": {
                            "qbv": {
                              "name": "qbv"
                            }
                          },
                          "inouts": {},
                          "outputs": {
                            "qbv": {
                              "name": "qbv"
                            }
                          },
                          "name": "repeat_cs4id_bbNysf",
                          "operands": {
                            "iteration": {
                              "rename_params": {},
                              "body": [
                                {
                                  "function": "if",
                                  "params": {
                                    "condition": {
                                      "expr": "Eq(is_parametrized[len(operands_1qubit) + i2],1)"
                                    },
                                    "port_size": {
                                      "expr": "num_qubits"
                                    }
                                  },
                                  "function_params": {
                                    "parameters": [],
                                    "input_decls": {},
                                    "output_decls": {}
                                  },
                                  "control_states": [],
                                  "inputs": {},
                                  "inouts": {
                                    "qbv": {
                                      "name": "qbv"
                                    }
                                  },
                                  "outputs": {},
                                  "name": "if_cs4id_yTcGF5",
                                  "operands": {
                                    "then": {
                                      "rename_params": {},
                                      "body": [
                                        {
                                          "function": {
                                            "name": "operands_2qubit",
                                            "index": {
                                              "expr": "i2"
                                            }
                                          },
                                          "params": {
                                            "angle": {
                                              "expr": "angle_params[num_qubits*sum(is_parametrized[0:len(operands_1qubit)])+                                                                                            +len(connectivity_map)*sum(is_parametrized[len(operands_1qubit):len(operands_1qubit)+i2])+len(angle_params)//reps*r+index] "
                                            }
                                          },
                                          "function_params": {
                                            "parameters": [],
                                            "input_decls": {},
                                            "output_decls": {}
                                          },
                                          "control_states": [],
                                          "inputs": {},
                                          "inouts": {
                                            "q1": {
                                              "name": "qbv",
                                              "start": {
                                                "expr": "connectivity_map[index][0]"
                                              },
                                              "end": {
                                                "expr": "connectivity_map[index][0]+1"
                                              }
                                            },
                                            "q2": {
                                              "name": "qbv",
                                              "start": {
                                                "expr": "connectivity_map[index][1]"
                                              },
                                              "end": {
                                                "expr": "connectivity_map[index][1]+1"
                                              }
                                            }
                                          },
                                          "outputs": {},
                                          "name": "operands_2qubit_cs4id_oYyF9J",
                                          "operands": {}
                                        }
                                      ]
                                    },
                                    "else": {
                                      "rename_params": {},
                                      "body": [
                                        {
                                          "function": {
                                            "name": "operands_2qubit",
                                            "index": {
                                              "expr": "i2"
                                            }
                                          },
                                          "params": {
                                            "angle": {
                                              "expr": "0"
                                            }
                                          },
                                          "function_params": {
                                            "parameters": [],
                                            "input_decls": {},
                                            "output_decls": {}
                                          },
                                          "control_states": [],
                                          "inputs": {},
                                          "inouts": {
                                            "q1": {
                                              "name": "qbv",
                                              "start": {
                                                "expr": "connectivity_map[index][0]"
                                              },
                                              "end": {
                                                "expr": "connectivity_map[index][0]+1"
                                              }
                                            },
                                            "q2": {
                                              "name": "qbv",
                                              "start": {
                                                "expr": "connectivity_map[index][1]"
                                              },
                                              "end": {
                                                "expr": "connectivity_map[index][1]+1"
                                              }
                                            }
                                          },
                                          "outputs": {},
                                          "name": "operands_2qubit_cs4id_TpmoCU",
                                          "operands": {}
                                        }
                                      ]
                                    }
                                  }
                                }
                              ]
                            }
                          }
                        }
                      ]
                    }
                  }
                }
              ]
            }
          }
        }
      ],
      "local_handles": []
    },
    {
      "name": "main",
      "param_decls": {
        "t": {
          "kind": "array",
          "element_type": {
            "kind": "real"
          },
          "size": 12
        }
      },
      "port_declarations": {},
      "operand_declarations": {},
      "parameters": [],
      "body": [
        {
          "function": "molecule_hartree_fock",
          "params": {
            "molecule_problem": {
              "expr": "struct_literal(MoleculeProblem,mapping=FermionMapping.JORDAN_WIGNER,z2_symmetries=False,molecule=struct_literal(Molecule,atoms=[struct_literal(ChemistryAtom,element=1,position=struct_literal(Position,x=0.0,y=0.0,z=0.0)),struct_literal(ChemistryAtom,element=1,position=struct_literal(Position,x=0.0,y=0.0,z=0.735))],spin=1,charge=0),freeze_core=False,remove_orbitals=[])"
            }
          },
          "function_params": {
            "parameters": [],
            "input_decls": {},
            "output_decls": {}
          },
          "control_states": [],
          "inputs": {},
          "inouts": {},
          "outputs": {
            "qbv": {
              "name": "hf"
            }
          },
          "name": "molecule_hartree_fock_cs4id_1qtSdj",
          "operands": {}
        },
        {
          "function": "full_hea",
          "params": {
            "num_qubits": {
              "expr": "4"
            },
            "is_parametrized": {
              "expr": "[0, 1, 0]"
            },
            "connectivity_map": {
              "expr": "[[0, 1], [1, 2], [2, 3]]"
            },
            "reps": {
              "expr": "3"
            },
            "angle_params": {
              "expr": "t"
            }
          },
          "function_params": {
            "parameters": [],
            "input_decls": {},
            "output_decls": {}
          },
          "control_states": [],
          "inputs": {
            "x": {
              "name": "hf"
            }
          },
          "inouts": {},
          "outputs": {},
          "name": "full_hea_cs4id_bI8MUW",
          "operands": {
            "operands_1qubit": [
              {
                "rename_params": {},
                "body": [
                  {
                    "function": "X",
                    "params": {},
                    "function_params": {
                      "parameters": [],
                      "input_decls": {},
                      "output_decls": {}
                    },
                    "control_states": [],
                    "inputs": {
                      "target": {
                        "name": "q"
                      }
                    },
                    "inouts": {},
                    "outputs": {
                      "target": {
                        "name": "q"
                      }
                    },
                    "name": "X_cs4id_2uPoJg",
                    "operands": {}
                  }
                ]
              },
              {
                "rename_params": {},
                "body": [
                  {
                    "function": "RY",
                    "params": {
                      "theta": {
                        "expr": "angle"
                      }
                    },
                    "function_params": {
                      "parameters": [],
                      "input_decls": {},
                      "output_decls": {}
                    },
                    "control_states": [],
                    "inputs": {
                      "target": {
                        "name": "q"
                      }
                    },
                    "inouts": {},
                    "outputs": {
                      "target": {
                        "name": "q"
                      }
                    },
                    "name": "RY_cs4id_uv0Biv",
                    "operands": {}
                  }
                ]
              }
            ],
            "operands_2qubit": [
              {
                "rename_params": {},
                "body": [
                  {
                    "function": "CX",
                    "params": {},
                    "function_params": {
                      "parameters": [],
                      "input_decls": {},
                      "output_decls": {}
                    },
                    "control_states": [],
                    "inputs": {
                      "target": {
                        "name": "q1"
                      },
                      "control": {
                        "name": "q2"
                      }
                    },
                    "inouts": {},
                    "outputs": {
                      "target": {
                        "name": "q1"
                      },
                      "control": {
                        "name": "q2"
                      }
                    },
                    "name": "CX_cs4id_SJt8Hc",
                    "operands": {}
                  }
                ]
              }
            ]
          }
        }
      ],
      "local_handles": [
        {
          "name": "hf"
        }
      ]
    }
  ],
  "types": [],
  "classical_functions": [
    {
      "name": "cmain",
      "param_decls": {},
      "body": [
        {
          "name": "vqe_result",
          "var_type": {
            "kind": "vqe_result"
          }
        },
        {
          "invoked_expression": {
            "function": "vqe",
            "params": {
              "hamiltonian": {
                "expr": "molecule_problem_to_hamiltonian(struct_literal(MoleculeProblem,mapping=FermionMapping.JORDAN_WIGNER,z2_symmetries=False,molecule=struct_literal(Molecule,atoms=[struct_literal(ChemistryAtom,element=1,position=struct_literal(Position,x=0.0,y=0.0,z=0.0)),struct_literal(ChemistryAtom,element=1,position=struct_literal(Position,x=0.0,y=0.0,z=0.735))],spin=1,charge=0),freeze_core=False,remove_orbitals=[]))"
              },
              "maximize": {
                "expr": "false"
              },
              "initial_point": {
                "expr": "[]"
              },
              "optimizer_name": {
                "expr": "Optimizer.COBYLA"
              },
              "max_iteration": {
                "expr": "30"
              },
              "tolerance": {
                "expr": "0.0"
              },
              "step_size": {
                "expr": "0.0"
              },
              "skip_compute_variance": {
                "expr": "false"
              },
              "alpha_cvar": {
                "expr": "1.0"
              }
            },
            "target_function": "main",
            "target_params": {
              "t": {
                "expr": "runtime_params"
              }
            }
          },
          "assigned_variable": "vqe_result"
        },
        {
          "name": "molecule_result",
          "var_type": {
            "kind": "struct_instance",
            "name": "MoleculeResult"
          }
        },
        {
          "invoked_expression": {
            "expr": "molecule_ground_state_solution_post_process(struct_literal(MoleculeProblem,mapping=FermionMapping.JORDAN_WIGNER,z2_symmetries=False,molecule=struct_literal(Molecule,atoms=[struct_literal(ChemistryAtom,element=1,position=struct_literal(Position,x=0.0,y=0.0,z=0.0)),struct_literal(ChemistryAtom,element=1,position=struct_literal(Position,x=0.0,y=0.0,z=0.735))],spin=1,charge=0),freeze_core=False,remove_orbitals=[]),vqe_result)"
          },
          "assigned_variable": "molecule_result"
        },
        {
          "saved_variable": "molecule_result"
        }
      ]
    }
  ],
  "constants": [],
  "constraints": {
    "max_gate_count": {}
  },
  "execution_preferences": {
    "amplitude_amplification": {
      "iterations": []
    },
    "random_seed": 2122779782,
    "backend_preferences": {
      "backend_service_provider": "IBM Quantum",
      "backend_name": "aer_simulator",
      "provider": {}
    }
  },
  "preferences": {
    "custom_hardware_settings": {
      "basis_gates": [
        "cy",
        "cz",
        "z",
        "s",
        "rx",
        "ry",
        "t",
        "sx",
        "cx",
        "id",
        "tdg",
        "x",
        "y",
        "p",
        "sdg",
        "h",
        "u2",
        "rz",
        "u",
        "sxdg",
        "u1"
      ]
    },
    "random_seed": 3667516783
  }
}
from classiq import construct_chemistry_model, synthesize
from classiq.applications.chemistry import Molecule, MoleculeProblem
from classiq.applications.chemistry import HEAParameters, ChemistryExecutionParameters
from classiq.execution import OptimizerType

from classiq import execute

molecule = Molecule(
    atoms=[("H", (0.0, 0.0, 0.0)), ("H", (0.0, 0.0, 0.735))],
)
gs_problem = MoleculeProblem(
    molecule=molecule,
    mapping="jordan_wigner",
)

ansatz_parameters = HEAParameters(
    reps=3,
    num_qubits=4,
    connectivity_map=[(0, 1), (1, 2), (2, 3)],
    one_qubit_gates=["x", "ry"],
    two_qubit_gates=["cx"],
)
execution_params = ChemistryExecutionParameters(
    optimizer=OptimizerType.COBYLA,
    max_iteration=30,
)

model = construct_chemistry_model(
    chemistry_problem=gs_problem,
    use_hartree_fock=True,
    ansatz_parameters=ansatz_parameters,
    execution_parameters=execution_params,
)
qprog = synthesize(model)

result = execute(qprog)

The output circuit:

alt text

[1] Abhinav Kandala, Antonio Mezzacapo, Kristan Temme, Maika Takita, Markus Brink, Jerry M. Chow, Jay M. Gambetta Hardware-efficient variational quantum eigensolver for small molecules and quantum magnets. Nature 549, 242 (2017).